zoukankan      html  css  js  c++  java
  • kmp

     1 #include<stdio.h>
     2 #include<string.h>
     3 
     4 char a[1000010];
     5 char b[10010];
     6 int next[10010];
     7 int A,B;
     8 void getnext()
     9 {
    10     int i=0,j=-1;
    11     next[0]=-1;
    12     while(i<B)
    13     {
    14         if(j==-1||b[i]==b[j])   next[++i]=++j;
    15         else   j=next[j];
    16     }
    17 }
    18 
    19 int kmp_i()
    20 {
    21     int i,j;
    22     i=j=0;
    23     getnext();
    24     while(i<A)
    25     {
    26         if(j==-1||a[i]==b[j])
    27         {
    28             i++;j++;
    29         }
    30         else
    31             j=next[j];
    32         if(j==B)
    33             return i-j;
    34     }
    35     return 0;
    36 }
    37 
    38 int kmp_c()
    39 {
    40     int i,j,t;
    41     i=j=t=0;
    42     getnext();
    43     while(i<A)
    44     {
    45         
    46          if(j==-1||a[i] == b[j])
    47          {
    48              j++;
    49              i++;
    50          }
    51          else j=next[j];
    52 
    53           if(j == B)
    54           {
    55              t++;
    56               j = next[j];
    57           }
    58     }
    59     return t;
    60 }
    61 
    62 int main()
    63 {
    64     int n;
    65     scanf("%d",&n);
    66     while(n--)
    67     {
    68         scanf("%s%s",b,a);
    69         A=strlen(a);
    70         B=strlen(b);
    71         printf("%d\n",kmp_c());
    72     }
    73     return 0;
    74 }

    poj 1961

    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
     1 #include<stdio.h>
     2 #include<string.h>
     3 
     4 int len;
     5 int next[1000005];
     6 char a[1000005];
     7 
     8 void getnext()
     9 {
    10     int i,j;
    11     memset(next,0,sizeof(next));
    12     next[0]=-1;
    13     i=0;
    14     j=-1;
    15     while(i<len)
    16     {
    17         if(j==-1||a[i]==a[j])
    18             next[++i]=++j;
    19         else
    20             j=next[j];
    21     }
    22 }
    23 
    24 int main()
    25 {
    26     int i,j,n;
    27     n=0;
    28     while(scanf("%d",&len),len)
    29     {
    30         scanf("%s",a);
    31         getnext();
    32         printf("Test case #%d\n",++n);
    33         for(i=2;i<=len;i++)
    34         {
    35             j=i-next[i];
    36             if(i%j==0&&i/j>1)
    37                 printf("%d %d\n",i,i/j);
    38         }
    39         printf("\n");
    40     }
    41     return 0;
    42 }
  • 相关阅读:
    通过pwndbg看看c中局部变量是如何在stack上放置的 此外 printf %n的作用终于弄明白了
    pip 安装过慢 使用清华源 加速
    mac 10.15.6 安装 IDA
    使用机器学习检测命令行混淆
    安全技能树简版
    栈溢出 hack 入门例子 hello world
    201116西瓜书机器学习系列---8、集成学习
    legend2---某些js代码电脑浏览器支持,手机浏览器不支持的调试
    legend2---做题页的每个题目对应的答案重点标颜色
    legend2---jquery重新渲染某元素
  • 原文地址:https://www.cnblogs.com/xiaofanke/p/3011114.html
Copyright © 2011-2022 走看看