题意:统计单串中从某个位置以前有多少重复的串

思路:kmp模板

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std; #define MaxSize 1000005 char str[MaxSize];
int next2[MaxSize]; void GetNext(char t[]){
int j,k,len;
j=;
k=-;
next2[]=-;
len=strlen(t);
while(j<len){
if(k==-||t[j]==t[k]){
++j;
++k;
next2[j]=k;
/*优化
if(t[j]!=t[k])next[j]=k;
else next[j]=next[k];
*/
}
else k=next2[k];
}
} int KMPIndex(char s[],char t[]){
int i,j,lens,lent;
i=j=;
lens=strlen(s);
lent=strlen(t); while(i<lens&&j<lent){
if(j==-||s[i]==t[j]){
++i;
++j;
}
else j=next2[j];
}
if(j>=lent)return i-lent;
else return -;
} int KMPCount(char s[],char t[]){//统计子串在主串中的出现次数,可重叠
int i,j,lens,lent,cnt;
i=j=;
lens=strlen(s);
lent=strlen(t);
cnt=; while(i<lens){
if(j==-||s[i]==t[j]){
++i;
++j;
}
else j=next2[j];
if(j==lent)++cnt;
}
return cnt;
} void KMPCount2(char t[]){//统计单串中从某个位置以前有多少重复的串
int i,lent,tmp;
lent=strlen(t); for(i=;i<=lent;++i){
tmp=i-next2[i];
if(i%tmp==&&i/tmp>)
printf("%d %d\n",i,i/tmp);
}
} int main(){
int n,m=; while(~scanf("%d",&n)&&n){
scanf("%s",str);
GetNext(str);
printf("Test case #%d\n",++m);
KMPCount2(str);
printf("\n");
}
return ;
}

hdu 1358 Period(kmp求一个串的重复子串)的更多相关文章

  1. poj 2406 Power Strings(kmp求一个串的重复子串)

    题意:重复子串次数 思路:kmp #include<iostream> #include<stdio.h> #include<string.h> using nam ...

  2. Hdu 1358 Period (KMP 求最小循环节)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目描述: 给出一个字符串S,输出S的前缀能表达成Ak的所有情况,每种情况输出前缀的结束位置和 ...

  3. [HDU 1358]Period[kmp求周期]

    题意: 每一个power前缀的周期数(>1). 思路: kmp的next. 每一个前缀都询问一遍. #include <cstring> #include <cstdio> ...

  4. HDU 1358 Period (kmp求循环节)(经典)

    <题目链接> 题目大意: 意思是,从第1个字母到第2字母组成的字符串可由某一周期性的字串(“a”) 的两次组成,也就是aa有两个a组成: 第三行自然就是aabaab可有两个aab组成: 第 ...

  5. HDU 1358 Period(KMP next数组运用)

    Period Problem Description For each prefix of a given string S with N characters (each character has ...

  6. hdu 1358 period KMP入门

    Period 题意:一个长为N (2 <= N <= 1 000 000) 的字符串,问前缀串长度为k(k > 1)是否是一个周期串,即k = A...A;若是则按k从小到大的顺序输 ...

  7. HDU 1358 Period KMP

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1358 求周期问题,简单KMP—— AC代码: #include <iostream> # ...

  8. HDU 1358 Period(KMP计算周期)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目大意:给你一串字符串,判断字符串的前缀是否由某些字符串多次重复而构成. 也就是,从第1个字母 ...

  9. hdu 1358 Period(KMP入门题)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

随机推荐

  1. putnik

    可将旅行商的路线看作是从n - 1号点出发, 跳着到0号点, 再折返走完之前跳过的点. 想到这个, 暴力就可以得50分. 正解是DP. f[i][j](i > j)表示, 从i开始跳, 并返回至 ...

  2. Ubuntu 16.04下使用Wine安装文件比对工具Beyond Compare 4

    说明: 1.使用的Wine版本是深度出品(Deepin),已经精简了很多没用的配置,使启动能非常快,占用资源小. 2.关于没有.wine文件夹的解决方法:在命令行上运行winecfg: 下载: (链接 ...

  3. mysql存储过程命令行批量插入N条数据命令

    原文:http://blog.csdn.net/tomcat_2014/article/details/53377924 delimiter $$ create procedure myproc () ...

  4. FastDFS 使用经验分享

    原文:http://www.ttlsa.com/fastdfs/fastdfs-experience-sharing/ 应用背景 文件被上传到FastDFS后Storage服务端将返回的文件索引(FI ...

  5. Silverlight 离线安装包

    直接下载地址 https://www.microsoft.com/getsilverlight/locale/en-us/html/Microsoft%20Silverlight%20Release% ...

  6. GO -- socket读取内容

    func handleRead(conn net.Conn, done chan string) { for { buf := make([]) reqLen, err := conn.Read(bu ...

  7. ant的安装和配置

    1.从官网下载bin源码 http://ant.apache.org/bindownload.cgi#Verify%20Releases 校验源码的完整性 2.直接把解压,然后把文件放入/usr/lo ...

  8. mac 下 virtualbox 配置全网通

    mac下virtualbox实现主机和虚拟机.虚拟机和外网互访的方案 全局添加Host-Only网络 Adapter IPv4 Address:192.168.56.1 IPv4 Network Ma ...

  9. HDU 4514并查集判环+最长路

    点击打开链接 题意:中文题...... 思路:先推断是否能成环,之前以为是有向图,就用了spfa推断,果断过不了自己出的例子,发现是无向图.并查集把,两个点有公共的父节点,那就是成环了,之后便是求最长 ...

  10. leetcode 题解 || Remove Nth Node From End of List 问题

    problem: Given a linked list, remove the nth node from the end of list and return its head. For exam ...