标签:size acm ++ sig 老师 多少 第一部分 lin struct
5
abc
aaaa
abc
abcc
12345
4
#include <iostream> #include<algorithm> #include<string> using namespace std; typedef unsigned long long ull; const ull mod1=1e9+7,mod2=1e9+3,k=131; struct has { ull x,y; }a[100001]; ull hash1(string s) { ull len=s.size(); ull ans=0; for(ull i=0;i<len;i++) { ans=(ans*k+(ull)s[i])%mod1; } return ans; } ull hash2(string s) { ull len=s.size(); ull ans=0; for(ull i=0;i<len;i++) { ans=(ans*k+(ull)s[i])%mod2; } return ans; } bool cmp(has a,has b) { return a.x>b.x; } int main() { int n; string s; cin>>n; int ans=0; for(int i=1;i<=n;i++) { cin>>s; a[i].x=hash1(s); a[i].y=hash2(s); } sort(a+1,a+n+1,cmp); for(int i=1;i<=n;i++) { if(a[i].x!=a[i+1].x||a[i].y!=a[i+1].y) ans++; } cout<<ans<<endl; return 0; }
3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN
1
3
0
字符串哈希思路
#include<stdio.h> #include<string.h> #include<iostream> #define ll unsigned long long using namespace std; ll hash[1000005],x,cf[1000005]; char a[1000005]; void read(ll &s,int &l) { s=l=0; char c=getchar(); while(!(c>=‘A‘&&c<=‘Z‘)) { c=getchar(); } while(c>=‘A‘&&c<=‘Z‘) { l++; s*=27; s+=c-‘A‘+1; c=getchar(); } } ll ask(int i,int j) { return hash[j]-hash[i-1]*cf[j-i+1]; } void work() { int i,l1,l2,s=0; read(x,l1); scanf("%s",a+1); l2=strlen(a+1); hash[1]=a[1]-‘A‘+1; for(i=2;i<=l2;i++) { hash[i]=hash[i-1]*27+a[i]-‘A‘+1; } for(i=1;i<=l2-l1+1;i++) { if(ask(i,i+l1-1)==x) { s++; } } printf("%d\n",s); } int main() { int t,i; for(cf[0]=1,i=1;i<=1000000;i++) { cf[i]=cf[i-1]*27; } scanf("%d",&t); while(t--) { work(); } return 0; }
banana band bee absolute acm # ba b band abc
2
3
1
0
#include <stdio.h> #include <string.h> #include <iostream> using namespace std; struct node{ int num; node* next[26]; node() { num=0; memset(next,0,sizeof(next)); } }; node* root=new node(); node* rt; int id,len; void bulit(string str) { rt=root; len=str.size(); for(int i=0;i<len;i++) { id=str[i]-‘a‘; if(rt->next[id]==NULL) rt->next[id]=new node(); rt=rt->next[id]; rt->num++; } } int query(string str) { rt=root; len=str.size(); for(int i=0;i<len;i++) { id=str[i]-‘a‘; if(rt->next[id]==NULL) return 0; rt=rt->next[id]; } return rt->num; } int main() { char str[100]; while(gets(str)) { if(str[0]==‘#‘)break; build(str); } while(gets(str)) { printf("%d\n",querry(str)); } return 0; }
2
3 2
3 4 5
1
5
4 1
4 6 5 6
3
Case #1:
4
3
Case #2:
4
#include <bits/stdc++.h> using namespace std; const int maxn=1e5+10; typedef long long LL; int ch[32 * maxn][2]; LL value[32 * maxn]; int node_cnt; inline void init() { node_cnt = 1; memset(ch[0],0,sizeof(ch)); } inline void Insert(LL x) { int cur = 0; for(int i = 32;i >= 0;--i) { int idx = (x >> i) & 1; if(!ch[cur][idx]) { memset(ch[node_cnt],0,sizeof(ch[node_cnt])); ch[cur][idx] = node_cnt; value[node_cnt++] = 0; } cur = ch[cur][idx]; } value[cur] = x; } inline LL Query(LL x) { int cur = 0; for(int i = 32;i >= 0;--i) { int idx = (x >> i) & 1; if(ch[cur][idx ^ 1]) cur = ch[cur][idx ^ 1]; else cur = ch[cur][idx]; } return value[cur]; } int main() { int t; cin>>t; int d=1; while(t--) { int n,m; scanf("%d%d",&n,&m); int i; init(); for(i=1;i<=n;i++) { LL x; scanf("%lld",&x); Insert(x); } printf("Case #%d:\n",d); for(i=1;i<=m;i++) { LL x; scanf("%lld",&x); printf("%lld\n",Query(x)); } d++; } return 0; }
标签:size acm ++ sig 老师 多少 第一部分 lin struct
原文地址:https://www.cnblogs.com/175426-hzau-zxjc-con/p/9403734.html