ACM hdu 3336 Count the string
【题意概述】
给定一个文本字符串,找出所有的前缀,并把他们在文本字符串中的出现次数相加,再mod10007,输出和。
【题目分析】
利用kmp算法的next数组 再加上dp
【存在疑惑】
在分析next数组和dp之间的关系,结论是 dp[i] = (dp[next[i]]+1); 搞不懂之间存在的联系
【AC】
#include <bits/stdc++.h>
int n,m,dp[],next[];
char s[];
void getnext() {
int i=,j=-;
next[]=-;
while(i<m) {
if(j==-||s[i]==s[j]) {
++i;++j;
next[i]=j;
} else
j=next[j];
}
}
int main() {
scanf("%d",&n);
while(n--)
{
int sum=;
scanf("%d",&m);
scanf("%s",s);
getnext();
memset(dp,,sizeof(dp));
for(int i=;i<=m;++i) {
dp[i]=(dp[next[i]]+)%;//不理解
sum=(sum+dp[i])%;
}
printf("%d\n",sum);
}
return ;
}
参考博客:https://www.cnblogs.com/yuelingzhi/archive/2011/08/03/2126346.html
ACM hdu 3336 Count the string的更多相关文章
- HDU 3336 Count the string(KMP的Next数组应用+DP)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3336 Count the string KMP
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...
- hdu 3336 Count the string KMP+DP优化
Count the string Problem Description It is well known that AekdyCoin is good at string problems as w ...
- HDU 3336 Count the string(next数组运用)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3336 Count the string 查找匹配字符串
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3336:Count the string(数据结构,串,KMP算法)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3336 Count the string -KMP&dp
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- 【HDU 3336 Count the string】
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- HDU 3336——Count the string
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
随机推荐
- 高级控件之Scrollview ( 滑动屏幕 ) 与 Imageview (滑动屏幕 切换图片)
ScrollView 的xml布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayo ...
- 实现Winform端窗体关闭后刷新html网页内容
一.首先要知道刷新网页的路径: frmPointEasyToBeat fpetBeat = new frmPointEasyToBeat(bookNoteId, userInfo.UserId); f ...
- CountDownLatch与thread-join()的区别
今天学习CountDownLatch这个类,作用感觉和join很像,然后就百度了一下,看了他们之间的区别.所以在此记录一下. 首先来看一下join,在当前线程中,如果调用某个thread的join方法 ...
- Oracle SQL Developer 免费的DB2客户端
问题地址:https://stackoverflow.com/questions/8600735/is-there-any-opensource-db2-client 软件地址:http://www. ...
- Ubuntu重装mysql错误解决
新搭建的服务器,先在Ubuntu上安装mariadb,后来由于很多权限问题,决定安装Mysql,在卸载过程中由于未卸载干净,导致mysql重装过程中出现了很多问题. Reading package l ...
- UOJ #30. 【CF Round #278】Tourists
Description Cyberland 有 n 座城市,编号从 1 到 n,有 m 条双向道路连接这些城市.第 j 条路连接城市 aj 和 bj.每天,都有成千上万的游客来到 Cyberland ...
- ●BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2002 题解: LCT 如果把弹跳的起点和终点连一条边,弹出去的与n+1号点连边, 则不难发现 ...
- 【BZOJ3531】【SDOI2014】旅行
题目传送门 题目大意:给定一棵无根树,每个节点有自己的类别和权值,现在给定两个类别相同的点,叫你求这2点路径上同类别节点的权值和/最大权值. 节点类别与权值会改变. 解题思路:考虑对每一个类别开一棵线 ...
- 【LSGDOJ1383】修改回文 dp
题目描述 为了跟踪所有的牛,农夫JOHN在农场上装了一套自动系统. 他给了每一个头牛一个电子牌号 当牛走过这个系统时,牛的名字将被自动读入. 每一头牛的电子名字是一个长度为M (1 <= M & ...
- 【bzoj4011 hnoi2015】落忆枫音
题目描述 「恒逸,你相信灵魂的存在吗?」 郭恒逸和姚枫茜漫步在枫音乡的街道上.望着漫天飞舞的红枫,枫茜突然问出这样一个问题. 「相信吧.不然我们是什么,一团肉吗?要不是有灵魂......我们也不可能再 ...