Count the string

Time Limit: 2000/1000 MS (Java/Others)

Memory Limit: 32768/32768 K (Java/Others)

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


解题心得:

  1. 题意就是给你一个字符串,问你字符串中和每个前缀匹配的子串有多少个,可以前缀和前缀自身匹配。
  2. 其实就是一个对于next数组的理解,next数组记录的就是当前字符为后缀匹配的前缀,所以很简单,直接next匹配的时候记录总数就可以了。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<string>
using namespace std;
const int maxn = 2e5+100;
const int MOD = 10007;
char s[maxn];
int Next[maxn]; int cal_next(int n){
int ans = 1;
int k = -1;
Next[0] = -1;
for(int i=1;i<n;i++){
ans++;//自身和自身匹配
ans %= MOD;
while(k > -1 && s[k+1] != s[i])
k = Next[k];
if(s[k+1] == s[i]){
k++;
ans++;
ans %= MOD;
}
Next[i] = k;
}
return ans%MOD;
} int main() {
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
scanf("%s",s);
int ans = cal_next(n);
printf("%d\n",ans);
}
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(next数组运用)

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

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

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

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

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

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

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

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

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

  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 KMP

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

  10. ACM hdu 3336 Count the string

    [题意概述] 给定一个文本字符串,找出所有的前缀,并把他们在文本字符串中的出现次数相加,再mod10007,输出和. [题目分析] 利用kmp算法的next数组 再加上dp [存在疑惑] 在分析nex ...

随机推荐

  1. CentOS7.4搭建GitLab

    1.查看服务器环境 uname -a 2.下载安装包 [1]找到相应的最新版本的下载路径 网址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/e ...

  2. Oracle同义词。。。

    同义词 --私有同义词--私有同义词权限grant create synonym to scott;--创建私有同义词create synonym dp for scott.dept;--将查询dep ...

  3. ubuntu下apk的反编译

    今天调试一个程序的时候,因为需要上传数据到服务器,但是程序太过久远了,服务器上传的地址就忘记了,但是源码又不在我这里,因为要的急所以就被逼无奈的情况下想到了反编译,我用的是Linux Mint 14. ...

  4. 解决Maven依赖下载不全的问题

    背景描述 在日常学习过程中使用Maven构建SpringBoot+SpringCloud服务时,有时会使用非正式版的SpringBoot和SpringCloud(非正式版是指不是最终发布的版本,而是测 ...

  5. hibernate课程 初探单表映射3-3 对象类型

    本节简介: 1 简介对象类型(重点是音视频blob类型) 2 demo(对图片的写入数据库与读取) 1 简介对象类型 映射类型 java类型 标准sql类型 mysql类型 oracle类型 bina ...

  6. Day6 盒模型

    Day6  盒模型  1.一.标准盒模型(w3c盒模型)        1)组成部分:        content + padding + border + margin           内容  ...

  7. Django之model基础(查询补充)

    学习完简单的单表查询外,是远远不够的,今天我们对查询表记录做一个补充,接下来来看看基于对象的跨表查询.基于双下划线的跨表查询,聚合查询和分组查询,F查询与Q查询. 比如我们有如下一张表,在model中 ...

  8. img IE下支持最大宽度

    border:0 none; max-width: 560px; height:auto; width:expression(this.width > 600 ? "600px&quo ...

  9. substring、slice、substr的区别

    首先定义一个变量便于下面测试:var str = "xx351223441";   substring: str.substring(form,to):从字符串里截取下标为form ...

  10. thinkphp搜索实现

    视图: <html lang="zh-cn"><head> <meta charset="UTF-8"><title& ...