UVALive 6507 Passwords

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

解题:好高深的hash啊

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int maxn = 205;
 5 const int base = 233;
 6 const int mod = 1e9+7;
 7 char str[maxn][20010];
 8 LL hs[maxn][20010],B[20010];
 9 int n;
10 unordered_map<LL,int>ump;
11 void init(){
12     B[0] = 1;
13     for(int i = 1; i < 20010; ++i)
14         B[i] = B[i-1]*base%mod;
15 }
16 LL calc(int i,int L,int R){
17     return ((hs[i][R] - hs[i][L-1]*B[R - L + 1])%mod + mod)%mod;
18 }
19 void solve(){
20     ump.clear();
21     for(int i = 0; i < n; ++i){
22         for(int j = 1,len = strlen(str[i] + 1); j <= len; ++j){
23             hs[i][j] = (hs[i][j-1]*base + str[i][j])%mod;
24             ump[hs[i][j]]++;
25         }
26     }
27     int a = 0,b = 0;
28     for(int i = 0; i < n; ++i){
29         int len = strlen(str[i] + 1);
30         for(int j = 1; j <= len; ++j) --ump[hs[i][j]];
31         for(int j = 1; j <= len; ++j){
32             LL suffix = calc(i,len - j + 1,len);
33             if(!ump[suffix]) continue;
34             int x = 1,y = 1;
35             LL prefix = suffix;
36             for(int k = 2; k*j <= len; ++k){
37                 LL suffix2 = calc(i,len - j*k + 1,len);
38                 prefix = (prefix*B[j] + suffix)%mod;
39                 if(suffix2 != prefix) break;
40                 if(ump[prefix]) x = k;
41                 y = k;
42             }
43             if(x == y && x == 1) continue;
44             if(x == y) --x;
45             if(j*(x + y) > a + b){
46                 a = x*j;
47                 b = y*j;
48             }
49         }
50         for(int j = 1; j <= len; ++j) ump[hs[i][j]]++;
51     }
52     printf("%d %d\n",a,b);
53 }
54 int main(){
55     int kase;
56     init();
57     scanf("%d",&kase);
58     while(kase--){
59         scanf("%d",&n);
60         for(int i = 0; i < n; ++i)
61             scanf("%s",str[i] + 1);
62         solve();
63     }
64     return 0;
65 }
66 /*
67 2
68 3
69 abcabe
70 defg
71 bcabab
72 */

时间: 2024-10-28 10:56:35

UVALive 6507 Passwords的相关文章

UVALive 4848 Tour Belt

F - Tour Belt Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 4848 Description Korea has many tourist attractions. One of them is an archipelago (Dadohae in Korean), a cluster of small islands sca

UVALive 6467 Strahler Order 拓扑排序

这题是今天下午BNU SUMMER TRAINING的C题 是队友给的解题思路,用拓扑排序然后就可以了 最后是3A 其中两次RE竟然是因为: scanf("%d",mm); ORZ 以后能用CIN还是CIN吧 QAQ 贴代码了: 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <iostre

UVALive 7077 Little Zu Chongzhi&#39;s Triangles (有序序列和三角形的关系)

这个题……我上来就给读错了,我以为最后是一个三角形,一条边可以由多个小棒组成,所以想到了状态压缩各种各样的东西,最后成功了……结果发现样例过不了,三条黑线就在我的脑袋上挂着,改正了以后我发现N非常小,想到了回溯每个棍的分组,最多分5组,结果发现超时了……最大是5^12 =  244,140,625,厉害呢…… 后来想贪心,首先想暴力出所有可能的组合,结果发现替换问题是一个难题……最后T T ,我就断片了.. 等看了别人的办法以后,我才发现我忽视了三角形的特性,和把数据排序以后的特点. 如果数据从

Gym 100299C &amp;&amp; UVaLive 6582 Magical GCD (暴力+数论)

题意:给出一个长度在 100 000 以内的正整数序列,大小不超过 10^ 12.求一个连续子序列,使得在所有的连续子序列中, 它们的GCD值乘以它们的长度最大. 析:暴力枚举右端点,然后在枚举左端点时,我们对gcd相同的只保留一个,那就是左端点最小的那个,只有这样才能保证是最大,然后删掉没用的. UVaLive上的数据有问题,比赛时怎么也交不过,后来去别的oj交就过了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&qu

UVALive 6511 Term Project

Term Project Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ID: 651164-bit integer IO format: %lld      Java class name: Main 解题:强连通分量 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1

UVALive 6508 Permutation Graphs

Permutation Graphs Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ID: 650864-bit integer IO format: %lld      Java class name: Main 解题:逆序数 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long l

UVALive 2659+HUST 1017+ZOJ 3209 (DLX

UVALive 2659 题目:16*16的数独.试了一发大白模板. /* * @author: Cwind */ //#pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <map> #include <algorithm> #include <cstdio> #include <cstring> #include

UVALive 5545 Glass Beads

Glass Beads Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ID: 554564-bit integer IO format: %lld      Java class name: Main Once upon a time there was a famous actress. As you may expect, she played mostly

Sicily 13460. Passwords

13460. Passwords Constraints Time Limit: 1 secs, Memory Limit: 256 MB Description Mirko is an evil plotting genius and has gotten hold of a list of all possible passwords for a certain user account. The first thing he noticed was all the passwords ar