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. android打开文件、保存对话框、创建新文件夹对话框(转载)

    转载地址:点击打开 这是一个简单的只有3个按钮的程序,3个按钮分别对应三种工作的模式(保存.打开和文件夹选择).封装的SimpleFileDialog.java的内容如下: package com.e ...

  2. 本地添加Maven管理

    Maven下载安装: 1 .Maven下载地址:http://maven.apache.org/ 2 .下载解压到本地指定目录:记住该路径! 3 .新建/修改环境变量-系统变量: 名:M2_HOME ...

  3. 比例缩放 on() prop() 正则表达式

    $('#banner-section').css('height',$(window).width() / 1900 * 490 ); $(window).resize(function(){    ...

  4. redis空间键详解

    前言 redis的空间键通知是在2.8.0版本以后加入的,客户端通过发布订阅的方式,订阅某个频道,接收通过某种方式影响redis中数据的事件. 目录: 1.空间键事件分类 2.如何启用redis的空间 ...

  5. 修改Oracle环境变量$PATH

    此次在创建公司的Oracle 标准化应用时,提到了添加$PATH,但没有发现对我很符合我的现况的方法,现记录下此次添加$PATH的方法: 首先查看$PATH中是否已存在我们需要的路径: 执行指令ech ...

  6. cout格式化输出 详解

    //在使用setf等库函数时使用 //在使用流操纵算子时使用 //using namespace std; //以下所有的setf()都有对应的unsetf()用于取消设置 //所有的setiosfl ...

  7. Log4j知识汇总

    Log4j在java开发中还是很常见的,而在日志系统里面也占有举足轻重的地位,想要做好日志相关的工作,了解log4j还是很必要的. 下面就针对 log4j的官方文档user-guide 进行翻译与整理 ...

  8. 新建framework的bundle资源 linker command failed with exit code 1解決

    enable bitcode 设为no

  9. 【CF1000C】Covered Points Count(离散化+差分)

    点此看题面 大致题意: 给出\(n\)条线段,分别求有多少点被覆盖\(1\)次.\(2\)次...\(n\)次. 正常的算法 好吧,这道题目确实有个很简单的贪心做法(只可惜我做的时候没有想到,结果想了 ...

  10. Linux常用的200个命令总结分类

    ●目录操作命令(6 个) ls tree pwd mkdir rmdir cd   ●文件操作命令(7 个) touch cp mv rm ln find rename   ●文件查看及处理命令(21 ...