题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336

如果你是ACMer,那么请点击看下

题意:求每一个的前缀在母串中出现次数的总和。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std; typedef long long LL;
const int N=200005;
const LL II=100000000;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0); int next[N],c[N],ans,len;
char str[N]; void getNext(char *p)
{
memset(c,0,sizeof(c));
int j=0,k=-1;
next[0]=-1;ans=0;
while(j<len)//len是p的长度
{
if(k==-1||p[j]==p[k])
{
if(k!=-1)
{
c[j]=c[k]+1;
ans+=c[j];
}
j++;
k++;
next[j]=k;
}
else
k=next[k];
}
} int main()
{
int i,j,T;
cin>>T;
while(T--)
{
scanf("%d%s",&len,str);
getNext(str);
printf("%d\n",(ans+len)%10007);
}
return 0;
}

HDU 3336 Count the string KMP的更多相关文章

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

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

  3. [HDU 3336]Count the String[kmp][DP]

    题意: 求一个字符串的所有前缀串的匹配次数之和. 思路: 首先仔细思考: 前缀串匹配. n个位置, 以每一个位置为结尾, 就可以得到对应的一个前缀串. 对于一个前缀串, 我们需要计算它的匹配次数. k ...

  4. HDU 3336 Count the string ( KMP next函数的应用 + DP )

    dp[i]代表前i个字符组成的串中所有前缀出现的次数. dp[i] = dp[next[i]] + 1; 因为next函数的含义是str[1]~str[ next[i] ]等于str[ len-nex ...

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

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

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

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

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

  8. HDU 3336 Count the string(next数组运用)

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

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

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

随机推荐

  1. Oracle 日期时间

    select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from goods t insert into goods (id,createdate) value ...

  2. C# Windows Sockets (Winsock) 接口 (转)

    在.Net中,System.Net.Sockets 命名空间为需要严密控制网络访问的开发人员提供了 Windows Sockets (Winsock) 接口的托管实现.System.Net 命名空间中 ...

  3. ie调试控制台

    function initConsole() { var console = document.createElement('div'); console.id = 'errorConsole'; d ...

  4. 在 Android 中 Intent 的概念及应用

    一.显式Intent: startActivity(new Intent(MainActivity.this, 类名.class));   二.隐式Intent: 1.在AndroidManiFest ...

  5. UrlEncode编码/UrlDecode解码

    public class encode {    public static void main(String[] args) throws UnsupportedEncodingException ...

  6. C语言选择法排序

    #include <stdio.h> int main() { int i, j, p, n, q; ] = {, , , , }; //对无序数组进行排序 ; i<; i++) { ...

  7. nfs:server is not responding,still trying 原因与解决

    方案(学自他人) nfs:server is not responding,still trying的解决方法 (2009-04-20 10:20) 方法1 : 我在arm上通过NFS共享文件时出现下 ...

  8. 安装好maven后,在cmd中运行mvn报一下的错误

    当然报错,你这个路径下并没有pom.xml文件.你可以运行这个命令: mvn -version.

  9. 禁用 Windows Azure 网站中的 ARR 实例关联

    编辑人员注释: 本博客文章由 Windows Azure 网站团队的项目经理 Erez Benari 撰写. 在 Windows Azure 网站中设置网站的多个实例是横向扩展网站的绝佳方式,Azur ...

  10. POJ-1003&1004

    这两题比较简单,就不做分析了,描述下题目,就上代码吧. [题目描述] 1003,其实就是求这个方程的最小n:1/2 + 1/3 + 1/4 + ... + 1/(n + 1) >= c: 100 ...