「CODEVS2875」RY哥查字典
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#include<map> #include<set> #include<cmath> #include<stack> #include<queue> #include<cstdio> #include<vector> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define mod 9875321 #define pi acos(-1) #define inf 1000000000 #define ll long long using namespace std; ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,m; bool mp[10000005]; char a[10005]; int gethash(char a[]) { int key=0; int l=strlen(a); for(int i=0;i<l;i++) key=(key*29+a[i])%mod; return key; } int main() { n=read(); for(int i=1;i<=n;i++) { scanf("%s",a); mp[gethash(a)]=1; } m=read(); for(int i=1;i<=m;i++) { scanf("%s",a); if(mp[gethash(a)])puts("Yes"); else puts("No"); } return 0; } |
自然溢出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#include<map> #include<set> #include<cmath> #include<stack> #include<queue> #include<cstdio> #include<vector> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define mod 9875321 #define pi acos(-1) #define inf 1000000000 #define ll long long using namespace std; ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,m; bool mp[10000005]; char a[10005]; int gethash(char a[]) { int key=0; int l=strlen(a); for(int i=0;i<l;i++) key=key*29+a[i]; return abs(key)%mod; } int main() { n=read(); for(int i=1;i<=n;i++) { scanf("%s",a); mp[gethash(a)]=1; } m=read(); for(int i=1;i<=m;i++) { scanf("%s",a); if(mp[gethash(a)])puts("Yes"); else puts("No"); } return 0; } |
Subscribe