hdu 1358 Period (KMP求循环次数)
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求循环次数)的更多相关文章
- Hdu 1358 Period (KMP 求最小循环节)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目描述: 给出一个字符串S,输出S的前缀能表达成Ak的所有情况,每种情况输出前缀的结束位置和 ...
- HDU 1358 Period (kmp求循环节)(经典)
<题目链接> 题目大意: 意思是,从第1个字母到第2字母组成的字符串可由某一周期性的字串(“a”) 的两次组成,也就是aa有两个a组成: 第三行自然就是aabaab可有两个aab组成: 第 ...
- [HDU 1358]Period[kmp求周期]
题意: 每一个power前缀的周期数(>1). 思路: kmp的next. 每一个前缀都询问一遍. #include <cstring> #include <cstdio> ...
- HDU 1358 Period KMP
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1358 求周期问题,简单KMP—— AC代码: #include <iostream> # ...
- HDU 1358 Period(KMP next数组运用)
Period Problem Description For each prefix of a given string S with N characters (each character has ...
- hdu 1358 period KMP入门
Period 题意:一个长为N (2 <= N <= 1 000 000) 的字符串,问前缀串长度为k(k > 1)是否是一个周期串,即k = A...A;若是则按k从小到大的顺序输 ...
- HDU 1358 Period(KMP计算周期)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目大意:给你一串字符串,判断字符串的前缀是否由某些字符串多次重复而构成. 也就是,从第1个字母 ...
- hdu 1358 Period(KMP入门题)
Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDU - 1358 - Period (KMP)
Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
随机推荐
- Adding basic files · lcobucci/jwt@aad22ed · GitHub
Skip to content Features Business Explore Marketplace Pricing This repository Sign in or Sign up ...
- ML面试1000题系列(61-70)
本文总结ML面试常见的问题集 转载来源:https://blog.csdn.net/v_july_v/article/details/78121924 61.说说共轭梯度法? @wtq1993,htt ...
- line-height的用法(一)
行高”顾名思意指一行文字的高度.具体来说是指两行文字间基线之间的距离. 从上到下四条线分别是顶线.中线.基线.底线,很像才学英语字母时的四线三格,我们知道vertical-align属性中有top.m ...
- font-family:黑体;导致css定义全部不起作用
css文件里font-family: "黑体";这句会导致后面的css定义全部不起作用了. 只要把font-family: "黑体"; 改成 font-fami ...
- qt获取本机用户名
//获取用户名 QString getUserName() { #if 1 QStringList envVariables; envVariables << "USERNAME ...
- $.ajax中contentType: “application/json” 的用法
不使用contentType: “application/json”则data可以是对象 $.ajax({ url: actionurl, type: "POST", datTyp ...
- op应用:官方,wifidog,portal,uci,luci,脚本,框架,usb
http://wiki.openwrt.org/doc/starthttp://downloads.openwrt.org/docs/buildroot-documentation.htmlhttp: ...
- jquery鼠标悬停突出显示
在线演示 本地下载
- pl/sql基础知识—过程快速入门
n 过程 过程用于执行特定的操作,当建立过程时,既可以指定输入参数(in),也可以指定输出参数(out),通过在过程中使用输入参数,可以将数据传递到执行部分:通过使用输出参数可以将执行部分的数据传递 ...
- react-cnode
感谢无私开源的程序员们~~~代码因为你们更加美腻~ //根index.js import React, { Fragment } from 'react'; import ReactDOM from ...