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) ...
随机推荐
- table-列组
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- js函数获取ev对象
今天工作中遇到一个问题,就是平时获取一个ev(event)对象时候一般直接在调用方法里面写一个ev参数,即可直接拿到这个对象,但是有时候会遇到函数调用不是直接加在一个dom对象的后面,如: var o ...
- 构建第一个Spring Boot2.0应用之集成mybatis、Druid(七)
一.环境: IDE:IntelliJ IDEA 2017.1.1 JDK:1.8.0_161 Maven:3.3.9 springboot:2.0.2.RELEASE 二.说明: 本文综合之 ...
- fstab 解析
某些时候当Linux系统下划分了新的分区后,需要将这些分区设置为开机自动挂载,否则,Linux是无法使用新建的分区的. /etc/fstab 文件负责配置Linux开机时自动挂载的分区. Window ...
- jquery UI_tabs
1.tab使用 <!doctype html> <html lang="en"> <head> <meta charset="u ...
- sk-learning(1)
sk-learning学习笔记(1) 简介 scikit learning 是一个python的机器学习库,内置许多机器学习的算法诸如svm.随机森林.逻辑回归.贝叶斯网络等算法.覆盖了分类.聚类.回 ...
- Excel如何显示隐藏列?
我们在工作中遇到excel表格数据太多比较负责,同时字段太多需要隐藏一些不重要的字段方便阅读和分析其他数据那么我们如何取消隐藏数据呢?隐藏列比较简单选中点隐藏就可以了,取消隐藏需要一些小的技巧才能灵活 ...
- CentOS系统下安装Redis
1. 安装C语言环境 yum install gcc-c++ 2.下载Redis安装包 http://download.redis.io/releases/redis-3.2.9.tar.gz 3.解 ...
- C# StreamWriter对像
用FileWriter来随机读取文件是个好主意,而用StreamWriter可以直接把字符串写入文件中,它处理重要的转换和向FileStream对像写入工作.创建StreamWriter有很多方法: ...
- 黑马基础阶段测试题:创建一个存储字符串的集合list,向list中添加以下字符串:”C++”、”Java”、” Python”、”大数据与云计算”。遍历集合,将长度小于5的字符串从集合中删除,删除成功后,打印集合中的所有元素
package com.swift; import java.util.ArrayList; import java.util.List; import java.util.ListIterator; ...