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数组运用)的更多相关文章

  1. 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) ...

  2. 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 ...

  3. hdu 3336:Count the string(数据结构,串,KMP算法)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. HDU 3336 Count the string 查找匹配字符串

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. hdu 3336 Count the string(next数组)

    题意:统计前缀在串中出现的次数 思路:next数组,递推 #include<iostream> #include<stdio.h> #include<string.h&g ...

  6. 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 ...

  7. HDU 3336 Count the string KMP

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...

  8. 【HDU 3336 Count the string】

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  9. HDU 3336——Count the string

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

随机推荐

  1. android PopupWindow 显示问题

    设置可点击,其余控件也可点击,等操作 setOutsideTouchable(true);   setBackgroundDrawable(new BitmapDrawable()); setTouc ...

  2. 动态规划 - 213. House Robber II

    URL: https://leetcode.com/problems/house-robber-ii/ You are a professional robber planning to rob ho ...

  3. 使用Office Online Server在线预览Office

    微软官方文档介绍 ⒈介绍 Office Online Server是 Office Web Apps Server 的升级版本,安装环境必须为两台Windows Server 2012 R2 或 Wi ...

  4. 统计分析与R软件-chapter2-3

    2.3 对象和它的模式与属性 R是一种基于对象的语言,R的对象包含了若干个元素作为其数据,另外还可以有一些特殊数据称为属性,并规定了一些特定操作(如打印.绘图).比如,一个向量是一个对象,一个图形也是 ...

  5. HDOJ 1166 敌兵布阵 (线段树)

    题目: Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Ti ...

  6. 解决ubuntu的gedit编辑器中文乱码的问题

    hello,本人 sky 又和大家见面了很多人在使用ubuntu系统时发现打开windows系统下面写的文档的话会发现乱码,是因为编码格式的问题windows系统下面是用GB2312等编码格式进行中文 ...

  7. python3+selenium入门13-操作cookie

    可以把cookie理解为自己账户的身份证.因为http协议是无状态的,上一个请求和下一个请求没有关系.但是有时需要有关联.比如登录之后,才能进行操作这样的设置.这个就是cookie在起作用.登录成功时 ...

  8. python3+requests库框架设计05-unittest单元测试框架

    unittest单元测试框架,主要由四部分组成:测试固件.测试用例.测试套件.测试执行器 测试固件(test fixture) 测试固件有两部分,执行测试前的准备部分setUp(),测试执行完后的清扫 ...

  9. IOT相关协议

    MQTT协议的入门 入门教程; 发布/订阅(Pub/Sub)模式,方便消息在传感器之间传递; 这意味着发布者和订阅者之间并不需要直接建立联系; 消息类型 MQTT拥有14种不同的消息类型: CONNE ...

  10. 在 mingw32 上编译 libvpx 1.7.0 时的注意事项

    in the vp8/common/theading.h Just need to add 1 line:#include <sys/types.h>before the last occ ...