zoukankan      html  css  js  c++  java
  • P3370 【模板】字符串哈希

    模板

    链接:https://www.luogu.org/problem/show?pid=3370

    题目描述

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

    输入输出格式

    输入格式:

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

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

    输出格式:

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

    输入输出样例

    输入样例#1:
    5
    abc
    aaaa
    abc
    abcc
    12345
    输出样例#1:
    4

    说明

    时空限制:1000ms,128M

    数据规模:

    对于30%的数据:N<=10,Mi≈6,Mmax<=15;

    对于70%的数据:N<=1000,Mi≈100,Mmax<=150

    对于100%的数据:N<=10000,Mi≈1000,Mmax<=1500

    样例说明:

    样例中第一个字符串(abc)和第三个字符串(abc)是一样的,所以所提供字符串的集合为{aaaa,abc,abcc,12345},故共计4个不同的字符串。

    没什么好说的~模板题~

    Codes:

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<algorithm>
     5 #define ll unsigned long long //溢出时自动对2^64取膜
     6 
     7 using namespace std;
     8 const int mod1 = 1020760212; //2333
     9 const int mod2 = 1000000007;
    10 const int N = 10000 + 5;
    11 const int base = 131; //一般取31,131,1313...
    12 int n,len,ans; 13 char s[N]; 14 ll Hash[N]; //注意不要用hash关键字 15 struct e{ 16 ll x,y; 17 }a[N]; 18 ll hash1(char *s){ 19 len = strlen(s); 20 Hash[0] = 0; 21 for(int i = 1;s[i];++ i){ 22 Hash[i] = (Hash[i - 1] * base + s[i] - 'a') % mod1; 23 } 24 return Hash[len - 1]; 25 } 26 ll hash2(char *s){ 27 len = strlen(s); 28 Hash[0] = 0; 29 for(int i = 1;s[i];++ i){ 30 Hash[i] = (Hash[i - 1] * base + s[i] - 'a') % mod2; 31 } 32 return Hash[len - 1]; 33 } 34 int cmp (e x,e y){ 35 if(x.x == y.x) return x.y < y.y; 36 return x.x < y.x; 37 } 38 int main(){ 39 scanf("%d",&n); 40 for(int i = 1;i <= n;++ i){ 41 scanf("%s",s); 42 a[i].x = hash1(s),a[i].y = hash2(s); 43 } 44 ans = 1; 45 sort(a + 1,a + 1 + n,cmp); 46 for(int i = 2;i <= n;++ i){ 47 if(a[i].x != a[i - 1].x || a[i].y != a[i - 1].y) //任意一个不满足即不同 48 ans ++; 49 } 50 cout << ans << ' '; 51 return 0; 52 }

     

  • 相关阅读:
    maven pom
    Amazon(vpc)对等网络
    AWS IAM用户启用MFA
    AWS系列-EC2实例镜像选择
    AWS系列-EC2实例添加磁盘
    AWS系列-AWS EC2实例类型改配(机器配置升级)
    AWS必要了解知识
    AWS系列-EC2默认限制说明
    AWS系列-S3实现文件服务页面展示
    AWS 区域与可用区
  • 原文地址:https://www.cnblogs.com/Loizbq/p/7642516.html
Copyright © 2011-2022 走看看