题意:给一个字典和一封邮件,判断是否邮件中的每个单词都出现在字典中。

题解:裸字符串hash,勤快点的用C++手写hash,嫌麻烦就直接java吧

View Code
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 char hash[1000007][60];
 6 const int mod=1000007;
 7 int fac[60];
 8 int getkey(char s[])
 9 {
10     int sum=0,i;
11     for(i=0;s[i]!='\0';i++)
12     {
13         sum+=s[i]*fac[i];
14         if(sum>=mod)
15             sum%=mod;
16     }
17     i=1;
18     while(hash[sum][0]!='\0'&&strcmp(hash[sum],s)!=0)
19     {
20         sum+=i*i;
21         if(sum>=mod)
22             sum%=mod;
23     }
24     return sum;
25 }
26 int main()
27 {
28     int n;
29     fac[0]=1;
30     for(int i=1;i<60;i++)
31     {
32         fac[i]=fac[i-1]*i;
33         if(fac[i]>=mod)
34             fac[i]%=mod;
35     }
36     while(scanf("%d ",&n)!=EOF)
37     {
38         memset(hash,'\0',sizeof(hash));
39         char s[100];
40         for(int i=0;i<n;i++)
41         {
42             gets(s);
43             strcpy(hash[getkey(s)],s);
44         }
45         scanf("%d ",&n);
46         int ca=0;
47         while(n--)
48         {
49             bool flag=true;
50             while(gets(s),strcmp(s,"-1")!=0)
51             {
52                 if(strcmp(hash[getkey(s)],s)!=0)
53                 {
54                     if(flag)
55                         printf("Email %d is not spelled correctly.\n",++ca);
56                     flag=false;
57                     puts(s);
58                 }
59             }
60             if(flag)
61                 printf("Email %d is spelled correctly.\n",++ca);
62         }
63     }
64     puts("End of Output");
65     return 0;
66 }