zoukankan      html  css  js  c++  java
  • poj 2406 Power Strings【字符串+最小循环节的个数】

                                                                                                      Power Strings
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 38038   Accepted: 15740

    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

    题目分析:给你一个字符串,求出这个字符串的最小循环节的个数,如果该串没有子循环节,就只有自己本身一个循环节,当然结果就是1啦!
    像ababab:循环节就是ab,共有3个ab; 像abc:循环节就是abc,只有本身一个。

    code:
    #include <stdio.h>
    #include <string.h>
    
    char s[1000002];
    
    int main()
    {
        int len;
        while(scanf("%s", s)!=EOF)
        {
            if(s[0]=='.') break;
            len = strlen(s);
            for(int i=1; i<=len; i++)
                if(len%i==0)
                {
                    int ok = 1;
                    for(int j=i; j<len; j++){
                        if( s[j] != s[j%i] )
                        {
                            ok = 0;
                            break;
                        }
                    }
                    if(ok!=0){
                        printf("%d
    ", len/i);
                        break;
                    }
                }
        }
        return 0;
    }
    
    
    
    

  • 相关阅读:
    题解-CmdOI2019 口头禅
    题解-NOI2003 智破连环阵
    题解-CF1282E The Cake Is a Lie
    CF1288F Red-Blue Graph
    题解-洛谷P4229 某位歌姬的故事
    莫比乌斯反演
    [HNOI2008]越狱(bzoj1008)(组合数学+正难则反)
    [FJOI2007]轮状病毒(bzoj1002)(递推+高精度)
    矩阵快速幂
    高斯消元
  • 原文地址:https://www.cnblogs.com/yspworld/p/4750621.html
Copyright © 2011-2022 走看看