博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU - 3336 Count the string
阅读量:5217 次
发布时间:2019-06-14

本文共 2019 字,大约阅读时间需要 6 分钟。

题目:

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.InputThe 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.OutputFor each case, output only one number: the sum of the match times for all the prefixes of s mod 10007.Sample Input

24abab 8abababab
 

Sample Output

6 20 题意: 给一个字符串,问所有前缀在字符串中出现次数的和是多少? 解法: KMP+DP 求出next数组之后,得到一个位置的上一个匹配位置,然后做DP即可。 代码:
#include
using namespace std;#include
#include
#define maxn 210000int nextp[maxn];char s[maxn];int f[maxn];const int mod=10007;int pre[maxn];int getnext(char s[],int nextp[],int n){ nextp[0]=0;nextp[1]=0; for (int i=0;i
0){ j=nextp[j]; if (s[j]==s[i]) break; } if(s[j]==s[i]) { nextp[i+1]=j+1; pre[i]=j; } else nextp[i+1]=j; } int zans=0; for (int i=n-1;i>=0;i--){ zans=zans%mod+f[i]%mod; zans%=mod; if (pre[i]==-1) continue; f[pre[i]]+=f[i]; } return zans;}void deal(){ int n; scanf("%d",&n); scanf("%s",&s); int ans; ans=getnext(s,nextp,n); printf("%d\n",ans);}int main(){ int t; scanf("%d",&t); while(t--) deal(); return 0; }

 

转载于:https://www.cnblogs.com/xfww/p/7695114.html

你可能感兴趣的文章
DNS安装配置主从
查看>>
Kali Linux Web渗透测试手册(第二版) - 3.5 - 使用ZAP代理查看和修改请求
查看>>
22.引用指针
查看>>
【 js 基础 】【读书笔记】Javascript “继承”
查看>>
五.Hystrix请求缓存(request cache)
查看>>
Python+OpenCV图像处理之图像金字塔
查看>>
你的日志组件记录够清晰嘛?--自己开发日志组件 Logger
查看>>
简版的文件传输
查看>>
input(Text)控件作为填空输入,但运行后,有曾经输入的记录显示,用autocomplete="off"解决...
查看>>
Java多线程
查看>>
长春理工大学第十四届程序设计竞赛(重现赛)J
查看>>
统计一篇英文文章内每个单词出现频率,并返回出现频率最高的前10个单词及其出现次数...
查看>>
leetcode36 有效数独
查看>>
jQuery选择器和遍历的总结
查看>>
ThreadPerMessagePattern——关于匿名内部类
查看>>
osg 3ds模型加载与操作
查看>>
[转帖]IBM收购红帽价格是多少?是否会形成垄断企业?会存在什么不安因素?...
查看>>
[转]Whirlwind Tour of ARM Assembly
查看>>
python socket.error: [Errno 10054] 解决方法
查看>>
JavaScript 高级篇之函数 (五)
查看>>