kuangbin KMP G题
来源:互联网 发布:网络审核员天河 编辑:程序博客网 时间:2024/06/05 03:31
G - Power Strings
Given two strings a and b we define a*b to be their concatenation. For example, if a = “abc” and b = “def” then a*b = “abcdef”. If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = “” (the empty string) and a^(n+1) = a*(a^n).
Input
Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.
Output
For each s you should print the largest n such that s = a^n for some string a.
Sample Input
abcd
aaaa
ababab
.
Sample Output
1
4
3
题解:
KMP求最小循环节应用。
注意判断条件。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 1000000+100;char str[MAXN];int len;int nt[MAXN];void getNext(){ nt[0] = -1; int i = 0, j = -1; while (i <= len) { if (j == -1 || str[i] == str[j]) { nt[++i] = ++j; } else { j = nt[j]; } }}int main(){ while(scanf("%s",str)!=EOF) { if(str[0]=='.') break; len = strlen(str); getNext(); if(nt[len]==0) { cout<<1<<endl; continue; } int t = len-nt[len]; if(len%t) { cout<<1<<endl; } else cout<<len/t<<endl; } return 0;}
阅读全文
0 0
- kuangbin KMP G题
- kuangbin kmp B题
- kuangbin KMP D题
- kuangbin KMP E题
- kuangbin KMP H题
- [kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher G POJ 2406
- kuangbin kmp专题中的字符串暴力
- hdu 1711 kuangbin 字符串 A KMP入门
- [kuangbin带你飞]专题十六 KMP & 扩展KMP
- [kuangbin带你飞]搜索进阶 G
- kuangbin 数论 A题
- kuangbin 数论 E题
- [kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher J (kmp扩展)
- [kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher K (kmp扩展)
- [kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher L(kmp扩展)
- 新疆网赛G题(KMP+树状数组)
- [kuangbin带你飞]专题一 简单搜索 G POJ3087
- [kuangbin带你飞]专题四 最短路练习-G
- iOS11中的定位授权
- Nginx Learing
- 面向对象程序设计(Object-oriented programming,OOP)
- React native与phoneGap jquery mobile
- Andrew Ng笔记-week2
- kuangbin KMP G题
- int.class和Integer.class有何不同?
- 尝试Adam代替梯度下降
- OpenStack通过Restful api获取token
- 期货多品种多策略多周期组合
- oracle create tablespaces and create user ---cxl
- jenkins 从svn下载源码中途断网问题
- Retrofit请求参数注解字段说明
- 最火房卡欣欣十三水房卡棋牌源码下载