hdu3336-kmp应用其实也也可以不用
2018-02-05 21:46
239 查看
Count the string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12333 Accepted Submission(s): 5691
Problem Description
It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. For example:
s: "abab"
The prefixes are: "a", "ab", "aba", "abab"
For each prefix, we can count the times it matches in s. So we can see that prefix "a" matches twice, "ab" matches twice too, "aba" matches once, and "abab" matches once. Now you are asked to calculate the sum of the match times for all the prefixes. For "abab",
it is 2 + 2 + 1 + 1 = 6.
The answer may be very large, so output the answer mod 10007.
Input
The first line is a single integer T, indicating the number of test cases.
For each case, the first line is an integer n (1 <= n <= 200000), which is the length of string s. A line follows giving the string s. The characters in the strings are all lower-case letters.
Output
For each case, output only one number: the sum of the match times for all the prefixes of s mod 10007.
Sample Input
1
4
abab
Sample Output
6
#include <iostream> #include <cstring> #include<stdio.h>//不能用using namespace std或者把next命名改掉;因为next存在命名重复会CE编译却不报错,~~好可恶找了半天,咨询了一下别人才解决 int next[200003]; char T[200003]; int tlen; void getNext() { int j, k; j = 0; k = -1; next[0]=-1; while(j < tlen) if(k == -1 || T[j] == T[k]) next[++j] = ++k; else k = next[k]; } int main() { int t; scanf("%d",&t); int n; while(t--) { scanf("%d",&n); memset(next,0,sizeof(next)); // getchar();可以去掉的原因是%s不接受空串,所以不会影响T字符串的输入 scanf("%s",T); tlen = strlen(T); getNext(); int sum[200003]={0}; int summ=0; for(int i=2;i<=n;i++)//字符串下标是从0开始,在0,1下标之前不可能存在长度为1(最小了)的前缀和后缀相等的情况。next[i]代表在i位置前相等的最长前后缀//前后缀举例abaa:前缀{a,ab,aba}后缀{baa,aa,a}// { sum[next[i]]++; } for(int j=1;j<n;j++)//j代表前缀的长度 { summ+=sum[j]; summ%=10007; } printf("%d\n",(summ+n)%10007);//加n是因为每个前缀和本身都算一,前两个for循环求得是除了各个前缀以外相同的个数 } return 0; }
第二种方案
#include <iostream> #include <cstring> using namespace std; int main () { int n,m,i,j,k,l; char s[222222]; cin>>m; while(m--) { cin>>n; cin>>s;k=n%10007; for(i=1;i<n;i++) { l=i;j=0; //每次都是用j=0从第一个开始匹配,l=i代替i向后滑动 while(l<n&&j<n) if(s[l++]==s[j++]) k=(k+1)%10007; //成功一个就加一 else break; } cout<<k<<endl; } return 0; }
相关文章推荐
- hdu3336-kmp应用其实也也可以不用
- 同学,其实你可以不用周游世界……
- 世界杯Again!——并查集 and STL(其实完全可以不用并查集)
- 不用shareuserid和相同包名也可以共享资源,用于皮肤等需要资源共享的应用
- hdu3336 KMP应用
- 应用内核补丁而不用重启 - 梦想可以在某些时候成真了吗?
- 前面在《Asp.Net Forums中对.Net中序列化和反序列化的应用》一文中讲了,对于一些扩展属性,可以将字符串集合序列化为二进制,也可以从二进制反序列化为字符串集合。其实我一直有个疑问,对于asp.net中可以很容易实现,但是在asp中该如何?
- 应用 | 红黄蓝的虐童惨案,其实可以用机器学习等技术来避免
- 想在自己的android应用中获得当天的天气情况,这该怎么做呢?不用担心。中国国家气象局提供了获取所在城市天气预报信息接口。通过这个接口,我们就可以获取天气信息了。
- 同学,其实你可以不用周游世界……
- Binary String Matching string(stl) find()函数的应用 完全不用kmp
- hdu3336 Count the string(KMP应用)
- 不用的web应用可以运行在同一个tcp端口(比如80)上吗?tcp端口的唯一性体现在哪里
- 其实可以不用那么c++
- Javassist简单应用 - 不用学习JVM知识也可以修改class文件
- 其实可以不用那么c++
- dede列表页调用文章,其实是所有页面都可以调用,第一次应用sql标签
- 关于不用重新部署就可以应用更改的设置
- java__晓萌最近在做一个翻转图片的应用,你可能也知道,图片其实是由一个个的点组成的。于是,晓萌想先做一个可以翻转矩阵的程序,来解决他问题的核心部分。
- 数据结构实验之串一:KMP简单应用