zoukankan      html  css  js  c++  java
  • poj 2406 Power Strings【最小循环节】

    Power Strings
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 36926   Accepted: 15254

    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
    mp算法:
    #include<stdio.h>
    #include<string.h>
    #define MAX 1100000
    char str[MAX];
    int f[MAX];
    void getfail()//失配函数 此函数定义出的数组f表示当原字符串与目标字符串失配 
    {          //后可以根据f[]跳转到原字符串相应位置从而提高运算的效率 
    	int i,j;
    	int len=strlen(str);
    	j=0;
    	f[0]=f[1]=0;
    	for(i=1;i<len;i++)
    	{
    		j=f[i];
    		while(j && str[i] != str[j])
    		j = f[j];
    		f[i+1] = str[i] == str[j]?j+1:0;
    	}
    }
    int main()
    {
    	int n,m,j,i,s,t;
    	while(scanf("%s",str)&&str[0]!='.')
    	{
    		getfail();
    		int len = strlen(str);
    		if(len%(len-f[len]))//此处len-f[len]的意思是原字符串中最大循环节的长度 
    		printf("1
    ");  //len%(len-f[len]有余数则证明除去最大循环节所有的节数仍然有 
    		                //不可循环的字符
    		else             
    		printf("%d
    ",len/(len-f[len]));//如果原字符的最大循环节可以将整个字符串表示完毕 
    	}                                     //则输出循环次数  
    	return 0;
    }
    
  • 相关阅读:
    物理材质
    铰链joints
    unity 刚体
    扩展方法
    转换操作符方法(非基元类型转换)
    向方法传递可变数量的参数
    参数:可选参数和命名参数
    实例构造器与值类型和引用类型、类型构造器
    成员的可访问性,友元程序集,静态类
    如何删除github上项目的文件
  • 原文地址:https://www.cnblogs.com/tonghao/p/4670517.html
Copyright © 2011-2022 走看看