• 洛谷 P3370 【模板】字符串哈希


    洛谷 P3370 【模板】字符串哈希

    洛谷传送门

    题目描述

    如题,给定 NN 个字符串(第 ii 个字符串长度为 M_iM**i,字符串内包含数字、大小写字母,大小写敏感),请求出 NN 个字符串中共有多少个不同的字符串。

    #友情提醒:如果真的想好好练习哈希的话,请自觉,否则请右转PJ试炼场:)

    输入格式

    第一行包含一个整数 NN,为字符串的个数。

    接下来 NN 行每行包含一个字符串,为所提供的字符串。

    输出格式

    输出包含一行,包含一个整数,为不同的字符串个数。


    题解:

    代码:

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int maxl=1510;
    const int maxn=1e4+4;
    const int mod=1e9+7;
    int n;
    char s[maxl];
    int a[maxn],cnt;
    void hash(char s[])
    {
    	int ret=0;
    	int len=strlen(s);
    	for(int i=0;i<len;i++)
    		ret=(ret*31+s[i])%mod;
    	a[++cnt]=ret;
    }
    int main()
    {
    	scanf("%d",&n);
    	for(int i=1;i<=n;i++)
    	{
    		scanf("%s",s);
    		hash(s);
    	}
    	sort(a+1,a+cnt+1);
    	int siz=unique(a+1,a+cnt+1)-a-1;
    	printf("%d
    ",siz);
    	return 0;
    }
    
  • 相关阅读:
    HTML超链接应用场景
    String 字符串和StringBuffer的知识点总结
    猜数游戏代码
    MemoryLayout
    偏swift框架
    git的使用
    寄存器
    swift基础
    枚举
    安装Ubuntu 20.04 以后
  • 原文地址:https://www.cnblogs.com/fusiwei/p/14080681.html
Copyright © 2020-2023  润新知