zoukankan      html  css  js  c++  java
  • hdu1358 Period

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=1358

    题目:

    Period

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 7830    Accepted Submission(s): 3758


    Problem 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 file 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
     
    Recommend
    JGShining
     思路:kmp+最小循环节
     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 #define MP make_pair
     6 #define PB push_back
     7 typedef long long LL;
     8 typedef pair<int,int> PII;
     9 const double eps=1e-8;
    10 const double pi=acos(-1.0);
    11 const int K=1e6+7;
    12 const int mod=1e9+7;
    13 
    14 int nt[K];
    15 char sa[K],sb[K];
    16 
    17 void kmp_next(char *T,int *nt)
    18 {
    19     nt[0]=0;
    20     for(int i=1,j=0,len=strlen(T);i<len;i++)
    21     {
    22         while(j&&T[j]!=T[i])j=nt[j-1];
    23         if(T[j]==T[i])j++;
    24         nt[i]=j;
    25     }
    26 }
    27 int kmp(char *S,char *T,int *nt)
    28 {
    29     int ans=0;
    30     kmp_next(T,nt);
    31     int ls=strlen(S),lt=strlen(T);
    32     for(int i=0,j=0;i<ls;i++)
    33     {
    34         while(j&&S[i]!=T[j])j=nt[j-1];
    35         if(S[i]==T[j])j++;
    36         if(j==lt)
    37              ans++,j=0;
    38     }
    39     return ans;
    40 }
    41 int main(void)
    42 {
    43     int t,cnt=1;
    44     while(scanf("%d",&t)&&t)
    45     {
    46         scanf("%s",sa);
    47         kmp_next(sa,nt);
    48         printf("Test case #%d
    ",cnt++);
    49         for(int i=1;i<t;i++)
    50         {
    51             int k=i+1-nt[i];
    52             if(k==i+1||(i+1)%k!=0)
    53                 continue;
    54             printf("%d %d
    ",i+1,(i+1)/k);
    55         }
    56         printf("
    ");
    57     }
    58     return 0;
    59 }
  • 相关阅读:
    http协议介绍
    使用Bind提供域名解析服务
    .bash_profile和.bashrc的区别
    SNAT和DNAT
    9.Iptables与Firewalld防火墙
    ubuntu18.04.3新装系统安装QT5.14.1和环境配置
    【Navicat】如何激活成永久版本
    windows 安装配置mysql 8,以及远程连接访问
    fork子进程父进程死掉之后,getppid()不为1的解决办法
    ubuntu64运行32位程序安装过程
  • 原文地址:https://www.cnblogs.com/weeping/p/6669704.html
Copyright © 2011-2022 走看看