• poj-2406 Power Strings


    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 65854   Accepted: 27197

    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[i]表示以第i个字符结束的最大后缀和最大前缀相等。n-next[n]则为最小的循环节。若n%(n-next[n])==0,那么n/(n-next[n])则为循环节出现的最大次数。
     
    #include <iostream>
    #include<string.h>
    using namespace std;
    const int maxn = 1e6+10;
    char a[maxn];
    int ne[maxn];
    int main()
    {
        while(scanf("%s",a+1)&&a[1]!='.')
        {
            memset(ne,0,sizeof(ne));
            int len = strlen(a+1);
            for(int i=2,j=0;i<=len;i++)
            {
                while(j&&a[i]!=a[j+1]) j=ne[j];
                if(a[i]==a[j+1]) j++;
                ne[i]=j;
            }
            if(len%(len-ne[len])==0) printf("%d
    ",len/(len-ne[len]));
            else
                printf("1
    ");
        }
        return 0;
    }
  • 相关阅读:
    【bzoj1176】[Balkan2007]Mokia
    【bzoj1503】[NOI2004]郁闷的出纳员
    C#设置和获取系统环境变量
    结伙创业指南及翻脸法则
    Unity3D_(游戏)卡牌03_选关界面
    Unity3D_(游戏)卡牌02_主菜单界面
    Unity3D_(游戏)卡牌01_启动屏界面
    Unity3D_(游戏)跳一跳超简单制作过程
    如何将项目托管到Github上
    Android_(游戏)打飞机06:后续
  • 原文地址:https://www.cnblogs.com/wjc2021/p/11300065.html
Copyright © 2020-2023  润新知