您的位置:首页 > 其它

UVALive 6507 Passwords

2015-10-05 20:50 288 查看

Passwords

Time Limit: 3000ms
Memory Limit: 131072KB
This problem will be judged on UVALive. Original ID: 6507
64-bit integer IO format: %lld Java class name: Main

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 205;
const int base = 233;
const int mod = 1e9+7;
char str[maxn][20010];
LL hs[maxn][20010],B[20010];
int n;
unordered_map<LL,int>ump;
void init(){
B[0] = 1;
for(int i = 1; i < 20010; ++i)
B[i] = B[i-1]*base%mod;
}
LL calc(int i,int L,int R){
return ((hs[i][R] - hs[i][L-1]*B[R - L + 1])%mod + mod)%mod;
}
void solve(){
ump.clear();
for(int i = 0; i < n; ++i){
for(int j = 1,len = strlen(str[i] + 1); j <= len; ++j){
hs[i][j] = (hs[i][j-1]*base + str[i][j])%mod;
ump[hs[i][j]]++;
}
}
int a = 0,b = 0;
for(int i = 0; i < n; ++i){
int len = strlen(str[i] + 1);
for(int j = 1; j <= len; ++j) --ump[hs[i][j]];
for(int j = 1; j <= len; ++j){
LL suffix = calc(i,len - j + 1,len);
if(!ump[suffix]) continue;
int x = 1,y = 1;
LL prefix = suffix;
for(int k = 2; k*j <= len; ++k){
LL suffix2 = calc(i,len - j*k + 1,len);
prefix = (prefix*B[j] + suffix)%mod;
if(suffix2 != prefix) break;
if(ump[prefix]) x = k;
y = k;
}
if(x == y && x == 1) continue;
if(x == y) --x;
if(j*(x + y) > a + b){
a = x*j;
b = y*j;
}
}
for(int j = 1; j <= len; ++j) ump[hs[i][j]]++;
}
printf("%d %d\n",a,b);
}
int main(){
int kase;
init();
scanf("%d",&kase);
while(kase--){
scanf("%d",&n);
for(int i = 0; i < n; ++i)
scanf("%s",str[i] + 1);
solve();
}
return 0;
}
/*
2
3
abcabe
defg
bcabab
*/


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: