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> # ...
随机推荐
- [转载]各种在线api地址
J2SE1.7英文api地址: http://download.oracle.com/javase/7/docs/api/J2SE1.6英文api地址: http://download.oracle ...
- jQuery.merge 源码阅读
jQuery.merge(first,second) 概述 合并两个数组 返回的结果会修改第一个数组的内容——第一个数组的元素后面跟着第二个数组的元素. 参数 first:第一个待处理数组,会改变其中 ...
- oracle数据库存储过程中NO_DATA_FOUND不起作用?
1.首先创建一个表lengzijiantest,表中只有一个字段f_id CREATE TABLE LENGZIJIANTEST ( F_ID NUMBER NOT NULL ) 2.插入一条数据 i ...
- How to Programmatically Add/Delete Custom Options in Magento? - See more at: http://apptha.com/blog/
In this tutorial, I would like to help out Magento developers and clients with how to programmatical ...
- sass基本语法
sass是一种基于ruby语言开发的CSS预处理器.它可以使用变量,嵌套,混入,继承,运算,函数等编程语言具有的特性进行CSS的开发,使得CSS的开发变得简单粗暴清晰可维护. sass有两种后缀文件格 ...
- js 常用的一些函数
//设置默认焦点 var setFocus = function SetFocus(elementId) { document.onkeydown = function (even ...
- ajax 简单操作
<script> $(function () { //$("#send").click(function () { // $.get("JQuery.as ...
- django 基础入门(二)
一.关于数据库 1.首先django 1.9以上等版本不支持pymysql,因此需要做一些调整. 比如在settings.py 加入一段代码: import pymysql pymysql.insta ...
- linux分区工具fdisk的使用
fdisk是linux下的一块分区工具,使用简单方便,由于是对系统进行修改,需要root权限. 常用参数如下: fdisk -l : 列出所有的硬盘信息 直接传入设备名称可进入对该硬盘分区.例如,f ...
- 转:按需加载html 图片 css js
按需加载是前端性能优化中的一项重要措施,按需加载是如何定义的呢?顾名思义,指的是当用户触发了动作时才加载对应的功能.触发的动作,是要看具体的业务场景而言,包括但不限于以下几个情况:鼠标点击.输入文字. ...