zoukankan      html  css  js  c++  java
  • POJ2406 Power Strings

    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 42637   Accepted: 17787

    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.

    Source

    KMP求出next数组,设字符串长度为len,若len/(len-next[len])为整数,那么这个数就是答案。

    类似的题见http://www.cnblogs.com/SilverNebula/p/5550595.html

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<cmath>
     6 using namespace std;
     7 int next[1000010];
     8 char s[1000010];
     9 int len,ans;
    10 void getnext(){
    11     int i,j;
    12     next[0]=0;
    13     next[1]=0;
    14     for(i=1,j=0;i<len;i++){
    15         while(s[j]!=s[i] && j)j=next[j];
    16         if(s[j]==s[i])j++;
    17         next[i+1]=j;
    18     }
    19     return;
    20 }
    21 int main(){
    22     while(scanf("%s",s)!=EOF){
    23         if(s[0]=='.')break;
    24         len=strlen(s);
    25 //        memset(next,0,sizeof next);
    26         getnext();
    27         ans=0;
    28         if(len%(len-next[len])==0)
    29             printf("%d
    ",len/(len-next[len]));
    30         else printf("1
    ");
    31     }
    32     return 0;
    33 }
  • 相关阅读:
    windows加固
    linux加固
    加密与编码
    XSS笔记
    hackbar功能简介
    机器学习 | 从加法模型讲到GBDT算法
    把时间还给洞察,且看PPT调研报告自动生成攻略
    树莓派3折腾笔记
    看数学老师是如何一键搞定报告和试卷的
    博客笔记(blog notebook)
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/5677834.html
Copyright © 2011-2022 走看看