首页 > 其他分享 >POJ-2406-Power Strings

POJ-2406-Power Strings

时间:2023-02-02 12:01:57浏览次数:41  
标签:子串 string int len 2406 POJ str getp Strings



Power Strings

Time Limit : 6000/3000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 96   Accepted Submission(s) : 34


Problem Description


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


题目分析:

意思是有多少个子串构成比如说  aaaa = a^4   ababab=(ab)^3    abcd=(abcd)^1    应该明白了吧!也就是这个字符串能被他的最短子串构成   由kmp ,next表的特点知如果满足  len%(len-p[len])==0  就能由子串构成且子串个数就是 len/(len-p[len])




#include<cstdio>
#include<cstring>
const int M = 1000000+10;
char str[M];
int p[M],a[M],len;
void getp()
{
int i=0,j=-1;
p[i]=j;
while(i<len)
{
if(j==-1||str[i]==str[j])
{
i++;j++;
p[i]=j;
}
else j=p[j];
}
}
int main()
{
while(~scanf("%s",str)&&str[0]!='.')
{
len=strlen(str);
getp();
if(len%(len-p[len])==0)
printf("%d\n",len/(len-p[len]));
else
printf("1\n");
}
return 0;
}



标签:子串,string,int,len,2406,POJ,str,getp,Strings
From: https://blog.51cto.com/u_14235050/6033452

相关文章

  • poj-1458-Common Subsequence
    CommonSubsequenceTimeLimit:1000MSMemoryLimit:10000KTotalSubmissions:43207Accepted:17522DescriptionAsubsequenceofagivensequenceisthegivenseq......
  • poj 3984 迷宫问题(BFS+输出路径)
    迷宫问题TimeLimit: 1000MS MemoryLimit: 65536KTotalSubmissions: 11323 Accepted: 6776Description定义一个二维数组: intmaze[5][5]={ 0,1,......
  • POJ-2752-Seek the Name, Seek the Fame
    SeektheName,SeektheFameTimeLimit:4000/2000ms(Java/Other)   MemoryLimit:131072/65536K(Java/Other)TotalSubmission(s):43   AcceptedSubmis......
  • DO、DTO、BO、AO、VO、POJO定义和转换的正确姿势
    一、引言DO、DTO、BO、AO、VO、POJO的概念看似简单,但是想区分好或者理解好也不容易,本文简单梳理一下。通过各层POJO的使用,有助于提高代码的可读性和可维护性。----------......
  • 「POJ3076」Sudoku TJ
    「POJ3076」SudokuTJ为什么要用DLX呢?DFS他不香吗!题意:略去不讲(数独还没玩过?滚回去玩吧!)思路:DFS,判断方案是否可行。\(9\times9\)的数独问题中,我们采用的策略是:优......
  • POJ--3169 Layout(最短路)
    记录12:362023-1-30http://poj.org/problem?id=3169reference:《挑战程序设计竞赛(第2版)》2.5.6p111DescriptionLikeeveryoneelse,cowsliketostandcloseto......
  • Knight Moves POJ-1915 <bfs>
    KnightMovesTimeLimit: 1000MS MemoryLimit: 30000KTotalSubmissions: 37011 Accepted: 17105DescriptionBackgroundMrSomurolov,fabulous......
  • POJ--3723 Conscription(最小生成树)
    记录18:302023-1-29http://poj.org/problem?id=3723reference:《挑战程序设计竞赛(第2版)》2.5.6p109DescriptionWindyhasacountry,andhewantstobuildana......
  • <Web Navigation> stack-POJ1028
    WebNavigationTimeLimit: 1000MS MemoryLimit: 10000KTotalSubmissions: 41995 Accepted: 18687DescriptionStandardwebbrowserscontainf......
  • POJ 1185 炮兵阵地
    感觉上很难,确实自己做一开始没有想到是dp问题,同时没有进行剪枝,同时有一些准备工作没有做好没有提前将每一行的信息转成数字信息(做题经验不足)没有提前把每行可能的情况抽......