HDU 3336 Count the string(next数组运用)
Count the string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14096 Accepted Submission(s): 6462
Problem Description
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.
Input
The 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.
Output
For 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
题意:统计一个字符串的所有前缀在字符串中出现的次数的和
分析:next数组的运用,因为next数组可以表示以当前坐标的前一个字符结尾的字符串的前缀和后缀有多少个字符相等
举个例子,有一个字符串
abcdabdabcdab
i 0 1 2 3 4 5 6 7 8 9 10 11 12 13
a b c d a b d a b c d a b
nex[i] -1 0 0 0 0 1 2 0 1 2 3 4 5 6
dp[i] 0 1 1 1 1 2 2 1 2 2 2 2 3 3
dp[i]=dp[nex[i]]+1
dp[i]表示第i个字母出现次数(与前缀匹配情况下),+1代表的是前缀出现次数加1,dp总和即为前缀出现次数和
#include<iostream>
#include<string.h>
#define inf 10007
using namespace std;
int nex[200001], dp[200001];
void getnext(char s[],int len)
{
int i=0,j=-1;
nex[0]=-1;
while(i<len)
{
if(j==-1||s[i]==s[j])
nex[++i]=++j;
else j=nex[j];
}
}
int main()
{
int t,n;
scanf("%d",&t);
char s[200001];
while(t--)
{
scanf("%d%s",&n,s);
getnext(s,n);
int cnt=0;
dp[0]=0;
for(int i=1;i<=n;i++)
{
dp[i]=(dp[nex[i]]+1)%inf;
cnt+=dp[i]%inf;
}
printf("%d\n",cnt%inf);
}
return 0;
}
HDU 3336 Count the string(next数组运用)的更多相关文章
- 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+DP优化
Count the string Problem Description It is well known that AekdyCoin is good at string problems as w ...
- 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 查找匹配字符串
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3336 Count the string(next数组)
题意:统计前缀在串中出现的次数 思路:next数组,递推 #include<iostream> #include<stdio.h> #include<string.h&g ...
- 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 KMP
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...
- 【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 ...
随机推荐
- 【转】python模块分析之logging日志(四)
[转]python模块分析之logging日志(四) python的logging模块是用来写日志的,是python的标准模块. 系列文章 python模块分析之random(一) python模块分 ...
- Shell-find . -type f -name "*.jpg" -print | xargs tar -czvf images.tar.gz
查找所有的 jpg 文件,并且压缩它们: find . -type f -name "*.jpg" -print | xargs tar -czvf images.tar.gz
- Windows PowerShell 入門(9)-エラー編
対象読者 Windows PowerShellでコマンドレット操作ができる方 何らかのプログラミング経験があればなお良い 必要環境 Windows PowerShell エラーをリダイレクトする リダ ...
- NTSC、PAL、SECAM三大制式简介
NTSC.PAL.SECAM三大制式简介 NTSC.PAL和SECAM是全球三大主要的电视广播制式,这三种制式是不能互相兼容的,例如在PAL制式的电视上播放NTSC的视频,则影像画面将不能正常显示.下 ...
- 关于boost中enable_shared_from_this类的原理分析
首先要说明的一个问题是:如何安全地将this指针返回给调用者.一般来说,我们不能直接将this指针返回.想象这样的情况,该函数将this指针返回到外部某个变量保存,然后这个对象自身已经析构了,但外部变 ...
- 007_Chrome的Waterfall详解
一. 浏览器根据html中外连资源出现的顺序,依次放入队列(Queue),然后根据优先级确定向服务器获取资源的顺序.同优先级的资源根据html中出现的先后顺序来向服务器获取资源 Queueing. 出 ...
- VC++ 错误
1.error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup解决方 ...
- Light Oj 1005
题意: 从 n*n 的棋盘中放置 K 个 行和列不冲突的棋子 思路: 组合数学, 先选 k 个 行, k 个列, 就是 C(n,k) ^ 2; 然后 K 个棋子不相同, K ! 全排列 #includ ...
- Java+selenium chrome 常见的问题WebDriverException: unknown error: call function result missing 'value'
运行chrome浏览器 报错:"main" org.openqa.selenium.WebDriverException: unknown error: call function ...
- Windows上的程序员神器Cmder
用过Windows版本Git的都知道Git自带了Git Bash,这个在很大程度上满足了我的需求,随着Git的版本升级越来越好用 安装Cmder Cmder官网,它把conemu,msysgit和cl ...