关闭

POJ 2406 Power Strings [KMP+循环节规律]

163人阅读 评论(0) 收藏 举报
分类:

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

Hint

This problem has huge input, use scanf instead of cin to avoid time limit exceed.

题目大意:给一个字符串,要求这个字符串是由多少个连续的子串组成的;

题解:对于next数组, next[len]就是最后一个字符的位置,如果他有相同的子串,则            长度为len-next[len] 所以只需要把next数组求出来然后取余判断,如果没有相              同的,则只有他自己,即为1;

AC代码:

#include <iostream>
#include <cmath>
#include<cstdio>
#include <cstring>
#include <algorithm>
#define N 10000005
using namespace std ;
char a[N] ;
int len , n , next[N];
void kkk()
{
    int i , j ;
    i = 0;
    j = next[0] = -1;
    while(i < len)
    {
        while(j!=-1&&a[i]!=a[j])
             j = next[j];
        next[++i] = ++j;
    }
}

int main()
{
    while(scanf("%s",a))
    {
        if(a[0]=='.') break;
        len = strlen(a);
        memset(next,0,sizeof(next));
        kkk();
        int ans ;
        if(len%(len-next[len])==0) ans = len/(len-next[len]);
        else ans = 1;
        cout<<ans<<endl;
    }
    return 0 ;
}

0
0
查看评论

Power Strings (poj 2406 KMP)

Language: Default Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33205   Ac...
  • u014422052
  • u014422052
  • 2014-10-23 19:52
  • 1063

poj 2406 Power Strings (后缀数组)

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 45998   Accepted: 19230 Description...
  • clover_hxy
  • clover_hxy
  • 2016-12-28 20:04
  • 279

poj-2406-Power Strings

DescriptionGiven 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...
  • qq_21120027
  • qq_21120027
  • 2016-03-17 10:26
  • 175

POJ 2406 Power Strings [KMP+循环节]

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...
  • qq_33638791
  • qq_33638791
  • 2016-08-01 14:00
  • 107

POJ 2406 Power Strings(KMP+最小循环节)

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...
  • qq_37497322
  • qq_37497322
  • 2017-08-21 15:14
  • 110

poj 2406 Power Strings (KMP+最小循环节)

题目链接:  poj 2406 题目大意:   给出一个由某个串重复有限次得到的字符串                   求重复次数最多是多少,既找出最小重复子串 解题...
  • qq7366020
  • qq7366020
  • 2013-10-23 11:24
  • 752

POJ2406:Power Strings(后缀数组DC3)

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...
  • libin56842
  • libin56842
  • 2015-06-01 14:36
  • 2293

POJ 2406 Power Strings(循环节)

#include "string" #include "iostream" #include "cstdio" #include "cmath" #include "set" #includ...
  • secretbase_
  • secretbase_
  • 2015-04-06 21:56
  • 177

poj2406Power string

poj上不去,在百炼上搜的,都是同一道题,只是百炼上是中文,以后要多在百炼刷题了。 描述 给定两个字符串a和b,我们定义a*b为他们的连接。例如,如果a=”abc” 而b=”def”, 则a*b=”abcdef”。 如果我们将连接考虑成乘法,一个非负整数...
  • z8110
  • z8110
  • 2015-08-07 16:35
  • 663

POJ -2406 Power Strings(循环节,kmp专题)

A - Power Strings Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & ...
  • haut_ykc
  • haut_ykc
  • 2016-07-25 20:34
  • 252
    个人资料
    • 访问:112278次
    • 积分:4336
    • 等级:
    • 排名:第8280名
    • 原创:332篇
    • 转载:3篇
    • 译文:1篇
    • 评论:11条
    最新评论