Power Strings poj2406 (kmp 进阶 next数组使用)

原创 2017年08月12日 15:04:58

Power Strings
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 46647   Accepted: 19516

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.

#include <algorithm>
#include <iostream>
#include <cstring>
#include <iomanip>
#include <string>
#include <cstdio>
#include <cmath>
#include <map>

using namespace std;

#define For(i,a,b) for(i=a;i<=b;i++)
#define _For(i,a,b) for(i=b;i>=a;i--)
#define Out(x) cout<<x<<endl
#define Outdouble(x,a) cout<<fixed<<setprecision(a)<<1.0*x<<endl
#define pf printf
#define sf scanf
#define mset(arr,num) memset(arr,num,sizeof(arr))

#define ll long long
const ll inf = 550; ///
#define ok std::ios::sync_with_stdio(0)
#pragma comment(linker, "/STACK:102400000,102400000")

// #define debug
#if defined (debug)
---check---
#endif

/// ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^ ///

int next[1000100];
string s;

void get_next()
{
    int l = s.length();
    int i=0,j=-1;
    next[i] = j;
    while(i < l)
    {
        if(j == -1||s[i]==s[j])
        {
            i++;
            j++;
            //next[i] = j;
            if(s[i]!=s[j])
            {
                next[i] = j;
            }
            else
            {
                next[i]=next[j];
            } ///better
        }
        else
        {
            j = next[j];
        }
    }
}

int main()
{
    int i,num,n;
    while(cin>>s)
    {
        if(s[0] == '.')
        {
            return 0;
        }
        int len = s.length();
        get_next();
        if(len%(len-next[len]) == 0)
        {
            cout<<len/(len-next[len])<<endl;
        }
        else
        {
            Out(1);
        }
    }
    return 0;
}




版权声明:有错误麻烦赐教,感激不尽~~~(转载留言哦~)

POJ 2406-Power Strings(重复子串-KMP中的next数组)

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 47642   Accepted: 1...
  • MIKASA3
  • MIKASA3
  • 2017年04月11日 14:52
  • 247

KMP算法、next数组与前缀中的周期(相关题目:Power strings, poj2406)

在一个大的字符串S中查找字符串T,naive的算法时间复杂度为O(s * t)(这里s与t代表S的长度与T的长度);而应用KMP,时间复杂度为O(s + t)。 KMP算法的核心在于next...
  • mach7
  • mach7
  • 2014年02月26日 13:16
  • 2558

POJ - 2406 Power Strings(next数组妙用+后缀数组)

Power Strings Description Given two strings a and b we define a*b to be their concatenation. For...

【kmp】【后缀数组】【连续重复子串】Power Strings POJ2406

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 25921 ...

E - Power Strings POJ - 2406 (后缀数组||kmp)

这个题目是后缀数组专题里面的一道题目。结果就是照着后缀数组的思路写的果然超时了。结果上网一搜才发现kmp也能做,真是太死板了,做题。 思路:对于一个串如果它是某个串的重复得到的,那么当前串的某个后缀...

[KMP][后缀数组] poj2406 Power Strings

DescriptionGiven two strings a and b we define a*b to be their concatenation. For example, if a = “a...

KMP poj 2406 Power Strings 字符串的幂

题意:     给定两个字符串a和b,定义式子:a*b表示两个字符串的连接,例如:a = "abc", b = "def" 则:a*b = "abcdef"。如果将连乘看成乘法,则按照普遍的方法一个...

POJ 2406 Power Strings(KMP)

题目链接:点我Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" ...
  • cccruel
  • cccruel
  • 2017年08月05日 09:52
  • 77

[POJ]2406Power Strings KMP

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 49390   Accepted: 2...

[poj 2406] Power Strings KMP *nex

Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 40506 Accepte...
  • ALPS233
  • ALPS233
  • 2016年02月29日 14:07
  • 334
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Power Strings poj2406 (kmp 进阶 next数组使用)
举报原因:
原因补充:

(最多只允许输入30个字)