Problem - 1358

  KMP求循环节次数。题意是,给出一个长度为n的字符串,要求求出循环节数大于1的所有前缀。可以直接用KMP的方法判断是否有完整的k个循环节,同时计算出当前前缀的循环节的个数。

代码如下:

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = ;
char buf[N];
int next[N]; void getNext(char *str, int len) {
int i = , j = -;
next[] = -;
while (i <= len) {
while (j > - && str[i] != str[j]) j = next[j];
i++, j++;
next[i] = j;
}
} typedef pair<int, int> PII;
PII rec[N]; int main() {
int n, cas = ;
while (cin >> n && n) {
cin >> buf;
getNext(buf, n);
// for (int i = 0; i <= n; i++) cout << next[i] << ' '; cout << endl;
int cnt = ;
for (int i = ; i <= n; i++) {
if (i % (i - next[i])) continue;
if (i / (i - next[i]) == ) continue;
rec[cnt++] = PII(i, i / (i - next[i]));
}
sort(rec, rec + cnt);
printf("Test case #%d\n", cas++);
for (int i = ; i < cnt; i++) {
printf("%d %d\n", rec[i].first, rec[i].second);
}
puts("");
}
return ;
}

——written by Lyon

hdu 1358 Period (KMP求循环次数)的更多相关文章

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

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

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

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

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

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

  4. HDU 1358 Period KMP

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

  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 题目大意:给你一串字符串,判断字符串的前缀是否由某些字符串多次重复而构成. 也就是,从第1个字母 ...

  8. hdu 1358 Period(KMP入门题)

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

  9. HDU - 1358 - Period (KMP)

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

随机推荐

  1. vue+ElementUI项目中,上传控件为必填项,上传图片后清空提示信息

    (ps:以下是我在项目中遇到得问题及解决方法,希望对你们有帮助.如果还有其他方法,可以留言,谢谢) 一个表单页面,使用element-ui中el-upload上传图片,此项为必填项,然后写了校验规则, ...

  2. Javascript-正则表达式常用验证

    <div> <h1>一.判断中国邮政编码匹配</h1> <p>分析:中国邮政编码都是6位,且为纯数字</p> <div>邮政编码 ...

  3. HR招聘_(十)_招聘方法论(供应商管理)

    招聘和供应商长期合作,所以供应商管理也至关重要.供应商一般分为猎头,渠道,外包三类. 猎头 高端职位,高难度职位,急需职位和量大职位会和猎头公司合作共同完成招聘任务,猎头公司一般会有两种服务,猎头和R ...

  4. 解决安装编译工具gcc后无法连接mysql

    在安装编译工具gcc后: yum -y install make gcc g++ gcc-c++ libtool autoconf automake imake mysql-devel libxml2 ...

  5. listView中的button控件获取索引

    1.在listitem中初始化button的时候,给该button添加一个setTag方法,将此时的索引值传进去,然后在button的onclick事件中调用view的getTag方法,即可将list ...

  6. C++之正则表示,字符串是否为全字母或者全数字

    bool isLetter(std::string& inputtext){ tr1::regex reg("^[A-Za-z]+$"); bool bValid = tr ...

  7. npm上面实用的第三方工具包

    npm上面实用的第三方工具包 live-server 作用:为页面提供实时刷新重载的功能,并且能提供一个http服务器 官方地址:https://www.npmjs.com/package/live- ...

  8. NOIP模拟 17.8.14

    NOIP模拟17.8.14 (天宇哥哥考察细心程度的题) [样例解释]如果删去第一个 1:在[3,1,2]中有 3 个不同的数如果删去 3:在[1,1,2]中有 2 个不同的数如果删去第二个 1:在[ ...

  9. vsync信号产生与分发

    以下分析基于android 4.4代码 vsync信号的产生.分发涉及到以下几个类,先主要了解下他们各自的功能: HWComposer:产生hardware vsync,post fb VSyncTh ...

  10. QLabel添加Click信号

    使用自定义label来实现此功能 其他控件可参照此例. #include "customerqlabel.h" CustomerQlabel::CustomerQlabel(QWi ...