zoukankan      html  css  js  c++  java
  • 【KMPnxt数组应用】POJ2406Power Strings

    Power Strings
    Time Limit: 3000MS        Memory Limit: 65536K
    Total Submissions: 60872        Accepted: 25166
    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
    
    Waterloo local 2002.07.01
    T

    这道题就是求循环节的长度

    也就是个nxt数组应用

    答案就是n/(n-nxt[n]),注意n%(n-nxt[n])为真的时候要输出1

    这个是为什么呢

    动手画一画,nxt数组的特性就是这样,n-nxt[n]恰好就是一个循环节的长度

    因为我是习惯全部+1来写,所以就很玄学

    有一个最好要思考的细节

    为什么是nxt[n+1],因为在n+1的时候才会失配(正常的KMP应该是0到len-1,然后nxt[len]是答案)

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cstring>
     4 using namespace std;
     5 char a[1000000+100];
     6 int nxt[1000000+100],alen;
     7 void getnxt()
     8 {
     9     int j=1,k=0;
    10     nxt[1]=0;
    11     while(j<=alen)
    12     {
    13         if(k==0||a[j]==a[k])
    14             j++,k++,nxt[j]=k;
    15         else
    16             k=nxt[k];
    17     }
    18 }
    19 int main()
    20 {
    21     while(1)
    22     {
    23         memset(nxt,0,sizeof(nxt));
    24         scanf("%s",a+1);
    25         if(a[1]=='.')return 0;
    26         alen=strlen(a+1);
    27         getnxt();
    28         if(alen%(alen+1-nxt[alen+1]))puts("1"); 
    29         else printf("%d
    ",(alen)/((alen+1)-nxt[alen+1]));
    30     }
    31     return 0;
    32 }
  • 相关阅读:
    一个从excel导入数据错位问题
    Chrome F12 温故而知新 :因为重定向导致清空Network信息
    export / import 温故而知新
    Chrome Debugger 温故而知新:上下文环境
    微信导出表情包教程
    Oracle中判断字段是否为数字
    获取GridView中RowCommand的当前索引行
    Oracle SQL常用内置系统函数总结
    使用Task代替ThreadPool和Thread
    Visual Studio 使用及调试必知必会
  • 原文地址:https://www.cnblogs.com/Qin-Wei-Kai/p/10205698.html
Copyright © 2011-2022 走看看