zoukankan      html  css  js  c++  java
  • 洛谷 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;
    }
    
  • 相关阅读:
    UIKit, AppKit, 以及其他API在多线程当中的使用注意事项
    BOZJ-2590 优惠券
    P3620 [APIO/CTSC 2007] 数据备份
    矩阵乘法与快速幂
    CodeForces
    AtCoder
    CodeForces
    考试成绩和学号的(结构体)排序
    CodeForces
    Atcoder Beginner Contest 092 —— C题
  • 原文地址:https://www.cnblogs.com/fusiwei/p/14080681.html
Copyright © 2011-2022 走看看