• poj_2406Power Strings && poj_1961Period && poj_2752Seek the Name, Seek the Fame(KMP)


    Power Strings
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 26173   Accepted: 10973

    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

    next数组的一个性质:

    length-next[length]为此字符串的最小循环节,其中length为该字符串的长度,也即对于以'\0'作为第一个下标的字符串pat,pat[length]=‘\0’。
    另外,如果length % (length-next[length])==0,则此字符串的最大重复次数为 length / (length-next[length])。

    #include<iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    #pragma warning(disable : 4996)
    const int MAXN = 1000005;
    int Next[MAXN];
    char text[MAXN] = {0};
    char pat[MAXN] = {0};
    int length;
    
    void get_next()
    {
    	int i = 0, j = -1;
    	Next[0] = -1;
    	while(i < length)
    	{
    		if(j == -1 || pat[i] == pat[j])
    		{
    			i++;
    			j++;
    			Next[i] = j;
    		}
    		else
    		{
    			j = Next[j];
    		}
    	}
    }
    
    int main()
    {
    	while (scanf("%s", pat) != EOF)
    	{
    		if(strcmp(pat, ".") == 0)
    		{
    			break;
    		}
    		length = strlen(pat);
    		get_next();
    		if(length % (length - Next[length]) == 0)
    		{
    			printf("%d\n", length / (length - Next[length]));
    		}
    		else
    		{
    			printf("1\n");
    		}
    	}
    	return 0;
    }


    Period
    Time Limit: 3000MS   Memory Limit: 30000K
    Total Submissions: 10767   Accepted: 4953

    Description

    For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 <= i <= N) we want to know the largest K > 1 (if there is one) such that the prefix of S with length i can be written as AK ,that is A concatenated K times, for some string A. Of course, we also want to know the period K.

    Input

    The input consists of several test cases. Each test case consists of two lines. The first one contains N (2 <= N <= 1 000 000) – the size of the string S.The second line contains the string S. The input file ends with a line, having the 
    number zero on it.

    Output

    For each test case, output "Test case #" and the consecutive test case number on a single line; then, for each prefix with length i that has a period K > 1, output the prefix size i and the period K separated by a single space; the prefix sizes must be in increasing order. Print a blank line after each test case.

    Sample Input

    3
    aaa
    12
    aabaabaabaab
    0

    Sample Output

    Test case #1
    2 2
    3 3
    
    Test case #2
    2 2
    6 2
    9 3
    12 4
    #include<iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    #pragma warning(disable : 4996)
    const int MAXN = 1000005;
    int Next[MAXN];
    char pat[MAXN] = {0};
    int length;
    
    void get_next()
    {
    	int i = 0, j = -1;
    	Next[0] = -1;
    	while(i < length)
    	{
    		if(j == -1 || pat[i] == pat[j])
    		{
    			i++;
    			j++;
    			Next[i] = j;
    		}
    		else
    		{
    			j = Next[j];
    		}
    	}
    }
    
    int main()
    {
    	freopen("in.txt", "r", stdin);
    	int count = 1;
    	int n;
    	while (scanf("%d", &n) != EOF)
    	{
    		if(n == 0)
    		{
    			break;
    		}
    		scanf("%s", pat);
    		length = strlen(pat);
    		get_next();
    		printf("Test case #%d\n", count++);
    		for(int i = 1; i <= length; i++)
    		{
    			if(i % (i - Next[i]) == 0 && i / (i - Next[i]) > 1)
    			{
    				printf("%d %d\n", i, i / (i - Next[i]));
    			}
    		}
    		printf("\n");
    	}
    	return 0;
    }

    Seek the Name, Seek the Fame
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 9329   Accepted: 4447

    Description

    The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to their newly-born babies. They seek the name, and at the same time seek the fame. In order to escape from such boring job, the innovative little cat works out an easy but fantastic algorithm: 

    Step1. Connect the father's name and the mother's name, to a new string S. 
    Step2. Find a proper prefix-suffix string of S (which is not only the prefix, but also the suffix of S). 

    Example: Father='ala', Mother='la', we have S = 'ala'+'la' = 'alala'. Potential prefix-suffix strings of S are {'a', 'ala', 'alala'}. Given the string S, could you help the little cat to write a program to calculate the length of possible prefix-suffix strings of S? (He might thank you by giving your baby a name:) 

    Input

    The input contains a number of test cases. Each test case occupies a single line that contains the string S described above. 

    Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000. 

    Output

    For each test case, output a single line with integer numbers in increasing order, denoting the possible length of the new baby's name.

    Sample Input

    ababcababababcabab
    aaaaa
    

    Sample Output

    2 4 9 18
    1 2 3 4 5
    #include<iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    #pragma warning(disable : 4996)
    const int MAXN = 400005;
    int Next[MAXN];
    char pat[MAXN] = {0};
    int ans[MAXN] = {0};
    
    void get_next(int length)
    {
    	int i = 0, j = -1;
    	Next[0] = -1;
    	while(i < length)
    	{
    		if(j == -1 || pat[i] == pat[j])
    		{
    			i++;
    			j++;
    			Next[i] = j;
    		}
    		else
    		{
    			j = Next[j];
    		}
    	}
    }
    
    int main()
    {
    	freopen("in.txt", "r", stdin);
    	int temp;
    	int tot;
    	while (scanf("%s", pat) != EOF)
    	{
    		int len = strlen(pat);
    		tot = 0;
    		get_next(len);
    		temp = Next[len];
    		if(temp != 0)
    		{
    			ans[tot++] = temp;
    		}
    		while (Next[temp] > 0)
    		{
    			temp = Next[temp];
    			ans[tot++] = temp;
    		}
    		for(int i = tot - 1; i >= 0; i--)
    		{
    			printf("%d ", ans[i]);
    		}
    		printf("%d\n", len);
    	}
    	return 0;
    }






  • 相关阅读:
    aes加密
    获取当前系统的版本号
    解决eclipse中出现Resource is out of sync with the file system问题
    Mac系统打开命令行终端及查看操作系统版本号的方法
    android短信拦截
    android权限大全
    mac系统下的常用命令
    android 中 系统日期时间的获取
    ubuntu tor浏览器
    Python中的random模块
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5835078.html
Copyright © 2020-2023  润新知