KMP的next数组,对于next[i],是:1~i-1的最长的匹配的前缀和后缀的长度(也即在i位置匹配失败后,应该跳到的模式串的位置)

然后我们将所有满足要求的字串按照它的末尾位置分类。

 #include <cstdio>
#include <cctype>
#define M 10007
#define maxn 200010 int n;
char aa[maxn];
int f[maxn], dp[maxn]; void gn( int &v ) {
char ch, opt;
while(!isdigit(ch=getchar())) opt=ch;
v=ch-'';
while( isdigit(ch=getchar())) v=v*+ch-'';
if( opt=='-' ) v=-v;
}
void gc( char &ch ) {
while(!isalpha(ch=getchar()));
}
void getfail() {
f[] = f[] = ;
for( int i=,j; i<n; i++ ) {
j = f[i];
while( j && aa[i]!=aa[j] ) j=f[j];
f[i+] = aa[i]==aa[j] ? j+ : ;
}
}
int main() {
int T;
gn(T);
while( T-- ) {
gn(n);
for( int i=; i<n; i++ )
gc(aa[i]);
getfail();
int sum=;
dp[] = dp[] = ;
for( int i=; i<=n; i++ ) {
dp[i] = dp[f[i]]+(f[i]!=);
sum = (sum+dp[i])%M;
}
sum += n;
sum %= M;
printf( "%d\n", sum );
}
}

hdu 3336的更多相关文章

  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

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

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

  4. (KMP)Count the string -- hdu -- 3336

    http://acm.hdu.edu.cn/showproblem.php?pid=3336 Count the string Time Limit: 2000/1000 MS (Java/Other ...

  5. hdu 3336 kmp+next数组应用

    分析转自:http://972169909-qq-com.iteye.com/blog/1114968 十分易懂 题意:求字串中[前缀+跟前缀相同的子串]的个数? Sample Input 1 4 a ...

  6. HDU 3336 扩展kmp

    题目大意: 找到字符串中所有和前缀字符串相同的子串的个数 对于这种前缀的问题,通常通过扩展kmp来解决 其实吧这是我第一次做扩展kmp的题目,原来确实看过这个概念,今天突然做到,所以这个扩展kmp的模 ...

  7. HDU 3336 - Count the string(KMP+递推)

    题意:给一个字符串,问该字符串的所有前缀与该字符串的匹配数目总和是多少. 此题要用KMP的next和DP来做. next[i]的含义是当第i个字符失配时,匹配指针应该回溯到的字符位置. 下标从0开始. ...

  8. hdu 3336【Count the string】(KMP)

    一道字符串匹配的题目,仅仅借此题练习一下KMP 因为这道题目就是要求用从头开始的n个字符串去匹配原来的字符串,很明显与KMP中求next的过程很相似,所以只要把能够从头开始匹配一定个数的字符串的个数加 ...

  9. hdu 3336 Count the string(思维可水过,KMP)

    题目 以下不是KMP算法—— 以下是kiki告诉我的方法,好厉害的思维—— 就是巧用标记,先标记第一个出现的所有位置,然后一遍遍从标记的位置往下找. #include<stdio.h> # ...

  10. HDU 3336 (KMP next性质) Count the string

    直接上传送门好了,我觉得他分析得非常透彻. http://972169909-qq-com.iteye.com/blog/1114968 #include <cstdio> #includ ...

随机推荐

  1. 【IDEA】IDEA设置新建文件的模板

    今天在IDEA中新建JS文件的时候想着也像WebStorm一样可以显示作者和时间,所以就研究了下在IDEA中修改文件创建时的模板. 点击settings找到File and Code Template ...

  2. django框架<三>

    一.ORM操作  1.django orm创建数据库的方法 (1)指定连接pymysql(python3.x),先配置__init__.py import pymysql pymysql.instal ...

  3. 【读书笔记::深入理解linux内核】内存寻址

    我对linux高端内存的错误理解都是从这篇文章得来的,这篇文章里讲的 物理地址 = 逻辑地址 – 0xC0000000:这是内核地址空间的地址转换关系. 这句话瞬间让我惊呆了,根据我的CPU的知识,开 ...

  4. 利用json模块解析dict报错找不到attribute 'dumps'[python2.7]

    [背景] 环境: RHEL 7.3 版本: python2.7 [错误情况] 写了一个简单的python脚本 将dict转换为json 脚本如下: #!/usr/bin/python #-*- cod ...

  5. Oracle Spatial操作geometry方法

    Oracle Spatial中SDO_GEOMETRY类型: CREATE TYPE SDO_GEOMETRY AS OBJECT( SDO_GTYPE NUMBER,--几何类型,如点线面 SDO_ ...

  6. SQL:select case when 的用法

    CASE 可能是 SQL 中被误用最多的关键字之一.虽然你可能以前用过这个关键字来创建字段,但是它还具有更多用法.例如,你可以在 WHERE 子句中使用 CASE. 首先让我们看一下 CASE 的语法 ...

  7. LightOJ 1414 February 29(闰年统计+容斥原理)

    题目链接:https://vjudge.net/contest/28079#problem/R 题目大意:给你分别给你两个日期(第二个日期大于等于第一个日期),闰年的2月29日称为闰日,让你求两个日期 ...

  8. 三、ansible简要使用

    1.ansible服务器生成公钥与私钥 ssh-keygen -t rsa 2.拷贝ansible公钥到客户机上 ssh-copy-id -i root@192.168.1.1 3.添加主机到ansi ...

  9. Valid Parentheses——栈经典

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  10. WPF 获取指定文件的Icon

    C# var icon = System.Drawing.Icon.ExtractAssociatedIcon(@"filepath"); var m = new MemorySt ...