题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36131

题意:给出一个长度为n的字符串,要求找到一些i,满足说从1~i为多个个的重复子串构成,并输出子串的个数。

题解:对kmp中预处理的数组的理解

//作者:1085422276
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include<bits/stdc++.h>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
const int inf = ;
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
ll exgcd(ll a,ll b,ll &x,ll &y)
{
ll temp,p;
if(b==)
{
x=;
y=;
return a;
}
p=exgcd(b,a%b,x,y);
temp=x;
x=y;
y=temp-(a/b)*y;
return p;
}
//*******************************
int n,p[];char a[];
int main()
{
int oo=;
while(scanf("%d",&n)!=EOF)
{
if(n==)break;
scanf("%s",a+);
memset(p,,sizeof(p));
int j=;
for(int i=;i<=n;i++)
{
while(j>&&a[j+]!=a[i])j=p[j];
if(a[j+]==a[i])j++;
p[i]=j;
}
cout<<"Test case #"<<oo++<<endl;
for(int i=;i<=n;i++)
{
if(p[i]>&&i%(i-p[i])==){
cout<<i<<" "<<i/(i-p[i])<<endl;
}
}
cout<<endl;
}
return ;
}

代码

UVA 1328 - Period KMP的更多相关文章

  1. UVA - 1328 Period(循环节kmp)

    https://vjudge.net/problem/UVA-1328 题意 求每个前缀的最小循环节,要求至少循环两次且为完整的. 分析 求next数组,i-next[i]即为前缀i的最小循环节,再判 ...

  2. UVa 1328 Period

    数据范围较大,故用KMP求循环节 之后由小到大枚举长度范围,若该长度下有循环节就输出答案 还要注意输出格式.之前测试时候连着一串presentation error也是悲伤 #include<b ...

  3. UVa 1328 (KMP求字符串周期) Period

    当初学KMP的时候也做过这道题,现在看来还是刘汝佳的代码要精简一些,毕竟代码越短越好记,越不容易出错. 而且KMP的递推失配函数的代码风格和后面的Aho-Corasick自动机求失配函数的代码风格也是 ...

  4. POJ 1961 Period( KMP )*

    Period Time Limit: 3000MSMemory Limit: 30000K Total Submissions: 12089Accepted: 5656 Description For ...

  5. hdu oj Period (kmp的应用)

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

  6. hdu 1358 period KMP入门

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

  7. LA3026 - Period(KMP)

    For each prefix of a given string S with N characters (each character has an ASCII code between 97 a ...

  8. poj1961 Period kmp解决找字符串的最小循环节

    /** 题目:poj1961 Period 链接:http://poj.org/problem?id=1961 题意:求从1到i这个前缀(2<=i<=N) ,如果有循环节(不能自身单独一个 ...

  9. HDU1358 Period —— KMP 最小循环节

    题目链接:https://vjudge.net/problem/HDU-1358 Period Time Limit: 2000/1000 MS (Java/Others)    Memory Lim ...

随机推荐

  1. Source Insight 基本使用(1)-使用Source Insight查看Android Framework 源码

    一.下载framework源码: google已经把framework源码托管在了gitHub上: https://github.com/android/platform_frameworks_bas ...

  2. RTMP流媒体播放过程(转)

    本文描述了从打开一个RTMP流媒体到视音频数据开始播放的全过程. 注意:RTMP中的逻辑结构 RTMP协议规定,播放一个流媒体有两个前提步骤:第一步,建立一个网络连接(NetConnection):第 ...

  3. .net 的一个分词系统(jieba中文分词的.NET版本:jieba.NET)

    简介 平时经常用Python写些小程序.在做文本分析相关的事情时免不了进行中文分词,于是就遇到了用Python实现的结巴中文分词.jieba使用起来非常简单,同时分词的结果也令人印象深刻,有兴趣的可以 ...

  4. svn切换用户

    问题背景:你用一个用户更新了代码,此时想用另一个用户提交,这就涉及到一个svn切换用户的问题 1. 查看svn 的用户名,密码:找到用户名,密码文件,都是明文的,你可以看到例:linuxhjj@hjj ...

  5. 后台返回json数据,前台显示代码

    List list = "从DAL获取的数据集合" //取出分页标签html int pageIndex = context.Request["pageIndex&quo ...

  6. 在docker里面安装部署应用

    最近一直在做docker的安装打包工作,学到不少东西,在博客里记一下. 环境centos6 ,docker 基础镜象centos6 1.创建本地基础镜象,安装基础命令包 (1)Dockerfile,D ...

  7. xcode6 使用MJRefresh

    1. MJRefreshConst.m 里面 会报错: unknown type 'NSString'... 原因:  xcode6 取消.pch文件, 所以没有导入 foundation和uikit ...

  8. HTML快速入门4

    七.表单 1. 概述 建立交互式的站点,需要使用 HTML 表单,它可以让用户提供信息,并对此作出处理.可以建立类似 复选框.单选按钮及文本框的控件. 掌握表单的使用对 Active Server P ...

  9. mysql 启动错误

    错误提示如下: 150815 20:24:40 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended150815 20:2 ...

  10. Linux 高可用开源方案 Keepalived VS Heartbeat对比

    1)Keepalived使用更简单:从安装.配置.使用.维护等角度上对比,Keepalived都比Heartbeat要简单得多,尤其是Heartbeat2.1.4后拆分成3个子项目,安装.配置.使用都 ...