Kolya and Tandem Repeat - CF#253 (Div. 2)B (443B) 哈希或水题
来源:互联网 发布:php 明天时间戳 编辑:程序博客网 时间:2024/06/05 09:35
Kolya got string s for his birthday, the string consists of small English letters. He immediately added k more characters to the right of the string.
Then Borya came and said that the new string contained a tandem repeat of length l as a substring. How large could l be?
See notes for definition of a tandem repeat.
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number k (1 ≤ k ≤ 200) — the number of the added characters.
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
aaba2
6
aaabbbb2
6
abracadabra10
20
A tandem repeat of length 2n is string s, where for any position i (1 ≤ i ≤ n) the following condition fulfills: si = si + n.
In the first sample Kolya could obtain a string aabaab, in the second — aaabbbbbb, in the third — abracadabrabracadabra.
题意:在给定字符串后加任意长度为k的字符后,最长能形成多长的重复两次的字符串。
思路:用哈希是n方复杂度,其实n的三次方也可以过。
AC代码如下:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int M=100007,Hash[210],F[210];char s[210];bool solve(int a,int b,int c,int d){ return Hash[d]-Hash[c-1]*F[d-c+1]==Hash[b]-Hash[a-1]*F[b-a+1];}int main(){ int n,i,j,k,len,a,b,c,d,ans=0; scanf("%s%d",s,&k); len=strlen(s); F[0]=1; for(i=1;i<=len;i++) { Hash[i]=Hash[i-1]*M+s[i-1]; F[i]=F[i-1]*M; } for(i=1;i<=len+k;i++) for(j=i;j<=len+k;j++) { a=i;b=j;c=j+1;d=2*j-i+1; if(d>len+k) break; if(d<=len) if(solve(a,b,c,d)) ans=max(ans,d-a+1); else if(c<=len) if(solve(a,a+len-c,c,len)) ans=max(ans,d-a+1); else ans=max(ans,d-a+1); } printf("%d\n",ans);}
- Kolya and Tandem Repeat - CF#253 (Div. 2)B (443B) 哈希或水题
- CF 253B. Kolya and Tandem Repeat
- CF 443B(253B)Kolya and Tandem Repeat
- CF B. Kolya and Tandem Repeat
- Codeforces 443 B. Kolya and Tandem Repeat
- CodeForces-443B Kolya and Tandem Repeat
- CodeForces 443B Kolya and Tandem Repeat
- Codeforces 442B Kolya and Tandem Repeat(暴力)
- Kolya and Tandem Repeat
- Kolya and Tandem Repeat
- cf#324 Div.2 Problem B Kolya and Tanya
- cf443B Kolya and Tandem Repeat
- CF 584B Kolya and Tanya
- Codeforces Round #324 (Div. 2) B. Kolya and Tanya
- Codeforces Round #324 (Div. 2) B. Kolya and Tanya
- Codeforces Round #324 (Div. 2)B. Kolya and Tanya
- Codeforces Round #324 (Div. 2) B. Kolya and Tanya
- Codeforces Round #324 (Div. 2) B. Kolya and Tanya
- Anton and Letters - CF#253 (Div. 2)A (443A) 大水
- LCD驱动调试常见问题总结
- Python学习笔记(二):循环
- The resource is not on the build path of a java project
- HTML中的表单
- Kolya and Tandem Repeat - CF#253 (Div. 2)B (443B) 哈希或水题
- 解决Toast重复弹出,Toast单例模式
- poj 1198 二维树状数组
- 虚拟化KVM的使用 - mouse without borders
- c编译错误--error:stray \357 in program及其引发的字符编码概述
- Borya and Hanabi - CF#253 (Div. 2)C (442A) 状压暴力枚举
- css span提示框 练习(不能用div)
- 电子商务网站测试经验总结
- python url请求 乱码 urllib.quote 中文