HDU3336 Count the string(kmp
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
1
4
abab
Sample Output
6
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn=1e6+;
const int mod = 1e4+;
int net[maxn],len;
char c[maxn];
void getnext(){
int l = strlen(c);
int i = ,j = -;
net[]=-;
while(i<l){
if(j==-||c[i]==c[j]) net[++i]=++j;
else j = net[j];
}
}
int main()
{
int t;
cin>>t;
while(t--){
cin>>len>>c;
getnext();
int l = strlen(c),res = ;
for(int i = ;i < l;++i){
if(net[i]!=&&net[i]+!=net[i+])res += net[i];
}
cout<<(res+l+net[l])%mod<<endl;
}
return ;
}
HDU3336 Count the string(kmp的更多相关文章
- HDU3336 Count the string —— KMP next数组
题目链接:https://vjudge.net/problem/HDU-3336 Count the string Time Limit: 2000/1000 MS (Java/Others) ...
- hdu3336 Count the string kmp+dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 很容易想到用kmp 这里是next数组的应用 定义dp[i]表示以s[i]结尾的前缀的总数 那么 ...
- HDU3336 Count the string KMP 动态规划
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3336 题意概括 给T组数据,每组数据给一个长度为n的字符串s.求字符串每个前缀出现的次数和,结果mo ...
- 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 ...
- HDUOJ------3336 Count the string(kmp)
D - Count the string Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- hdu3336 Count the string 扩展KMP
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- HDU3336 Count the string 题解 KMP算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 题目大意:找出字符串s中和s的前缀相同的所有子串的个数. 题目分析:KMP模板题.这道题考虑 n ...
- [KMP][HDU3336][Count the string]
题意 计算所有S的前缀在S中出现了几次 思路 跟前缀有关的题目可以多多考虑KMP的NEXT数组 #include <cstdio> #include <cstring> #in ...
随机推荐
- yum update 更新失败
1.yum安装东西的时候,老是报:There are unfinished transactions remaining. You might consider running yum-complet ...
- 基于libcurl的restfull接口 post posts get gets
头文件 #pragma once #ifndef __HTTP_CURL_H__ #define __HTTP_CURL_H__ #include <string> #include &q ...
- Partial Dependence Plot
Partial Dependence就是用来解释某个特征和目标值y的关系的,一般是通过画出Partial Dependence Plot(PDP)来体现. PDP是依赖于模型本身的,所以我们需要先训练 ...
- Java项目框架搭建系列(Java学习路线)
前言: 已经工作4年,真是时间飞逝. 其实当你在一间公司工作一两年之后,公司用到的开发框架的基本使用你应该都会了. 你会根据一个现有项目A复制一下搭建出另外一个类似框架的项目B,然后在项目B上进行业务 ...
- xftp上传文件到虚拟机linux失败问题
如果想通过xftp上传文件到虚拟机linux时,可能会产生上传失败的问题 原因: 因为有些文件是只可读,所以要修改文件权限,可读可写,才可以上传成功. 解决方法: 第一种方法:用xftp连接虚拟机后, ...
- koa 基础(六)应用级路由中间件
1.应用级路由中间件 app.js /** * 应用级路由中间件 */ // 引入模块 const Koa = require('koa'); const router = require('koa- ...
- ccf 201612-4 压缩编码(DP)(100)
ccf 201612-4 压缩编码 问题分析: 解决本问题,首先需要知道哈夫曼编码.参见:哈夫曼编码_百度百科. 这是一个编码问题,似乎可以用哈夫曼编码来解决,但是略有不同的地方在于“每个字符的编码按 ...
- linux搭建ftp配置文件
# Example config file /etc/vsftpd/vsftpd.conf## The default compiled in settings are fairly paranoid ...
- Excel中,如何将人名按姓和名分开?
在Excel中,怎么将姓名分开呢? 用到三个函数: left函数:从文本字符串的左端开始,返回指定个数的字符: right函数:从字符串右端开始,返回指定个数的字符: len函数:返回文本串的字符数 ...
- [Java]手动构建SQL语法树(sql简单无嵌套)并输出与之对应的SQL语句之二
Entry入口 main中自顶向下手动创建了sql语法树 package com.hy; // 构建SQL语法树 public class Entry { public static void mai ...