hdu 4552 && 3336 【KMP算法中对next数组的理解】

来源:互联网 发布:欧洲历史书籍推荐知乎 编辑:程序博客网 时间:2024/06/14 01:16

这两道题几乎就是一样的就是题目不太容易理解,刚开始我也真是没看懂啥意思,




怪盗基德的挑战书

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1405    Accepted Submission(s): 647


Problem Description
  “在树最美丽的那天,当时间老人再次把大钟平均分开时,我会降临在灯火之城的金字塔前,带走那最珍贵的笑容。”这是怪盗基德盗取巴黎卢浮宫的《蒙娜丽莎的微笑》这幅画时,挑战书上的内容。
  但这次,怪盗基德的挑战书上出现了一串串小写字母“aaab sdfeeddd...”。柯南以小学生的眼睛,超凡高中生的头脑,快速统计各种字母频率,字符串长度,并结合挑战书出现的时间等信息,试图分析怪盗基德的意图。最后,他将线索锁定在字符串的循环次数上。并且进一步推理发现,从字符串的第一位开始,到第i位,形成该字符串的子串(c1, c2, c3 ... ci )。对于某一子串ci在该字符串中出现的次数记为ki,则全部子串的循环次数总和AIM = k1 + k2 + ... + ki + ... + kn,柯南发现,AIM恰好对应一个ASCII码!所以,只要把挑战书上的字符串转变成数字,再找到对应的ASCII码,就可以破解这份挑战书了!
  现在,你的任务就是把字符串转变成对应数字,因为ASCII码以及扩展ASCII码全部只有256个,所以,本题只要把结果对256取余即可。
 

Input
输入有多组测试数据;
每组测试数据只有一个字符串,由各种小写字母组成,中间无空格。
字符串的长度为L(0 < L <= 100000)。
 

Output
请计算并输出字符串的AIM值,每组数据输出一行。
 

Sample Input
aaaabab
 

Sample Output
66


         首先我们知道next数组中next[i]表示的是以第i个字符结尾的前缀中最长公共前后缀的长度,我们令num[i]表示以第i个字符结尾的前缀中所含有以第i个字符结尾的前缀的个数,那么显然有num[i]=num[next[i]]+1,求出num数组后累加即为答案 


/* *********************************************** ┆  ┏┓   ┏┓ ┆ ┆┏┛┻━━━┛┻┓ ┆ ┆┃       ┃ ┆ ┆┃   ━   ┃ ┆ ┆┃ ┳┛ ┗┳ ┃ ┆ ┆┃       ┃ ┆ ┆┃   ┻   ┃ ┆ ┆┗━┓ 马 ┏━┛ ┆ ┆  ┃ 勒 ┃  ┆       ┆  ┃ 戈 ┗━━━┓ ┆ ┆  ┃ 壁     ┣┓┆ ┆  ┃ 的草泥马  ┏┛┆ ┆  ┗┓┓┏━┳┓┏┛ ┆ ┆   ┃┫┫ ┃┫┫ ┆ ┆   ┗┻┛ ┗┻┛ ┆ ************************************************ */ #include<stdio.h>  #include<string.h>char S[200005];int next[200005],n,num[200005]={0};void GetNext(){int i=0,j=-1;next[0]=-1;j=next[i];while(i<n){if(j==-1||S[i]==S[j]){next[i+1]=j+1;i++; j++;}else{j=next[j];}} }int main(){while( ~scanf("%s",S) ){n=strlen(S);GetNext();int ans=0;for(int i=1; i<=n; i++){num[i]=(num[next[i]]+1)%256;ans=(ans+num[i])%256;}printf("%d\n",ans);}return 0;}





Count the string

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10822    Accepted Submission(s): 5039


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
14abab
 

Sample Output
6

/* *********************************************** ┆  ┏┓   ┏┓ ┆ ┆┏┛┻━━━┛┻┓ ┆ ┆┃       ┃ ┆ ┆┃   ━   ┃ ┆ ┆┃ ┳┛ ┗┳ ┃ ┆ ┆┃       ┃ ┆ ┆┃   ┻   ┃ ┆ ┆┗━┓ 马 ┏━┛ ┆ ┆  ┃ 勒 ┃  ┆       ┆  ┃ 戈 ┗━━━┓ ┆ ┆  ┃ 壁     ┣┓┆ ┆  ┃ 的草泥马  ┏┛┆ ┆  ┗┓┓┏━┳┓┏┛ ┆ ┆   ┃┫┫ ┃┫┫ ┆ ┆   ┗┻┛ ┗┻┛ ┆ ************************************************ */ #include<stdio.h>  #include<string.h>char S[200005];int next[200005],n,num[200005]={0};void GetNext(){int i=0,j=-1;next[0]=-1;j=next[i];while(i<n){if(j==-1||S[i]==S[j]){next[i+1]=j+1;i++; j++;}else{j=next[j];}} }int main(){int T;scanf("%d",&T);while(T--){scanf("%d",&n);scanf("%s",S);GetNext();int ans=0;for(int i=1; i<=n; i++){num[i]=(num[next[i]]+1)%10007;ans=(ans+num[i])%10007;}printf("%d\n",ans);}return 0;}























原创粉丝点击