标签:style io os ar for sp on c amp
每个字符串都哈希之后,二个之间二分出最长前缀!
需要注意的是不能用数组存,会暴,用vector就行了。
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map> #include<queue> #include<set> #include<cmath> #include<bitset> #define mem(a,b) memset(a,b,sizeof(a)) #define lson i<<1,l,mid #define rson i<<1|1,mid+1,r #define llson j<<1,l,mid #define rrson j<<1|1,mid+1,r #define INF 0x7fffffff #define maxn 100105 typedef long long ll; typedef unsigned long long ull; using namespace std; ull Hash,seed=131; vector<ull>v[maxn]; char s[maxn]; int main() { //freopen("1.txt","r",stdin); int t,ii=1; scanf("%d",&t); while(t--) { int n,m,i,j,a,b; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%s",s); v[i+1].clear(); for(j=0,Hash=0;s[j];j++) { Hash=Hash*seed+s[j]-'a'; v[i+1].push_back(Hash); } } scanf("%d",&m); printf("Case %d:\n",ii++); while(m--) { scanf("%d%d",&a,&b); int l=0,r=min(v[a].size(),v[b].size())-1,ans=0; while(l<=r) { int mid=(l+r)>>1; if(v[a][mid]==v[b][mid]) l=mid+1,ans=mid; else r=mid-1; } if(v[a][ans]!=v[b][ans]) printf("%d\n",ans); else printf("%d\n",ans+1); } } return 0; }
标签:style io os ar for sp on c amp
原文地址:http://blog.csdn.net/u011466175/article/details/39580235