hdu3366 Count the string
考虑dp[i]代表前缀s[1...i]出现的次数,必定有dp[nxt[i]] += dp[i]
倒着推就是了
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int T, n, nxt[200005], dp[200005], ans;
const int mod=10007;
char a[200005];
void mknxt(){
int k=0;
for(int i=2; i<=n; i++){
while(k && a[i]!=a[k+1]) k = nxt[k];
if(a[i]==a[k+1]) nxt[i] = ++k;
}
}
int main(){
cin>>T;
while(T--){
ans = 0;
scanf("%d", &n);
scanf("%s", a+1);
memset(nxt, 0, sizeof(nxt));
for(int i=1; i<=n; i++) dp[i] = 1;
mknxt();
for(int i=n; i>=1; i--)
dp[nxt[i]] = (dp[nxt[i]] + dp[i]) % mod;
for(int i=1; i<=n; i++)
ans = (ans + dp[i]) % mod;
printf("%d\n", ans);
}
return 0;
}
hdu3366 Count the string的更多相关文章
- Count the string[HDU3336]
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 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) ...
- HDUOJ------3336 Count the string(kmp)
D - Count the string Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- 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 ...
- Count the string -- HDOJ 3336
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdoj 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(next数组运用)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- (KMP)Count the string -- hdu -- 3336
http://acm.hdu.edu.cn/showproblem.php?pid=3336 Count the string Time Limit: 2000/1000 MS (Java/Other ...
- HDU 3336 Count the string 查找匹配字符串
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- C#运算符、控制流
1 运算符 1.1 一元运算符: -(负号).+(正号):可以省略 1.2 二元运算符: 优先级,*(乘)./(除).%(取余).+(加).-(减).=(赋值) 二元赋值运算符,=.+=.-= ...
- linux 下源码编译环境配置
yum install -y apr* autoconf automake bison bzip2 bzip2* compat* cpp curl curl-devel \ fontconfig fo ...
- touch事件总结
$("body").on("touchstart", function(e) { e.preventDefault(); startX = e.original ...
- 自己实现的简单的grid
12年在第一家公司的时候,有过很长一段时间在前端的使用研究上.一开始的时候使用ExtJs4.0 MVC 来开发前端,觉得里面的风转的组件非常好用,Panel.window.tree等等,简化了对于前端 ...
- Windows下用cmd命令安装及卸载服务[转]
第一种方法: 1. 开始 ->运行 ->cmd2. cd到C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727(Framework版本号按IIS配置) ...
- hihoCoder hiho一下 第一周 #1032 : 最长回文子串 (Manacher)
题意:给一个字符串,求最长回文子串的长度. 思路: (1)暴力穷举.O(n^3) -----绝对不行. 穷举所有可能的出现子串O(n^2),再判断是否回文O(n).就是O(n*n*n)了. (2)记录 ...
- getline()读入一整行
string line; getline(cin, line); cin不能读入空行,用getline可以读入空行.
- linux 命令——7 mv(转)
mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录. 1.命令格式: mv [选项] 源文件或目 ...
- <已解决> Eclipse启动失败
参考:http://stackoverflow.com/questions/15404964/starting-eclipse-results-in-failed-to-create-java-vir ...
- [转载]AngularJS入门教程02:AngularJS模板
是时候给这些网页来点动态特性了——用AngularJS!我们这里为后面要加入的控制器添加了一个测试. 一个应用的代码架构有很多种.对于AngularJS应用,我们鼓励使用模型-视图-控制器(MVC)模 ...