HDU 3336 Count the string KMP
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336
题意:求每一个的前缀在母串中出现次数的总和。
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的更多相关文章
- 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 ...
- 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 ...
- [HDU 3336]Count the String[kmp][DP]
题意: 求一个字符串的所有前缀串的匹配次数之和. 思路: 首先仔细思考: 前缀串匹配. n个位置, 以每一个位置为结尾, 就可以得到对应的一个前缀串. 对于一个前缀串, 我们需要计算它的匹配次数. k ...
- 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 ...
- 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) ...
- hdu 3336:Count the string(数据结构,串,KMP算法)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3336 Count the string 查找匹配字符串
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3336 Count the string(next数组运用)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3336 Count the string(思维可水过,KMP)
题目 以下不是KMP算法—— 以下是kiki告诉我的方法,好厉害的思维—— 就是巧用标记,先标记第一个出现的所有位置,然后一遍遍从标记的位置往下找. #include<stdio.h> # ...
随机推荐
- 使用jodconverter和swftools实现文件在线预览
参考:仿百度文库解决方案(四)——利用JODConverter调用OpenOffice.org服务转换文档为PDF 文档在线预览主要用到如下两个工具 1,安装openoffice(同时下载jodcon ...
- 1148 - Mad Counting(数学)
1148 - Mad Counting PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Limit: 32 MB M ...
- Java_java多线程下载-断点下载-超详细
基本原理:利用URLConnection获取要下载文件的长度.头部等相关信息,并设置响应的头部信息.并且通过URLConnection获取输入流,将文件分成指定的块,每一块单独开辟一个线程完成数据的读 ...
- Html.ActionLink(转载)
@Html.ActionLink 代码: <h2>HtmlHelper</h2>@Html.ActionLink("默认","Index" ...
- win7 64位下如何安装配置mysql-5.7.4-m14-winx64
win7 64位下如何安装配置mysql-5.7.4-m14-winx641. mysql-5.7.4-m14-winx64.zip下载 官方网站下载地址:http://dev.mysql.com/g ...
- URAL 1009 K-based Numbers
题目:Click here #include <bits/stdc++.h> using namespace std; typedef long long ll; const int IN ...
- struts jsp传值到action,乱码的解决方案
使用了Struts框架,前台写好了编码为utf-8 <%@ page language="java" contentType="text/html; charset ...
- hdoj 2222
http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道 AC自动机.....trie树的建立 和 AC自动机的查询,,可作模版... 解题思路:AC的应用 ...
- Regex阅读笔记(五)java操作篇
首先一个demo程序 Java的正则表达式包为java.util.regex,主要是使用其中的Pattern和Matcher. groupCount方法时候都可调用,而大多数方法都必须在匹配尝试成功之 ...
- SQL——找出某一字段中内容相同的数据
SELECT columnName from dbo.tableName group by columnName having count(*)>1