zoukankan      html  css  js  c++  java
  • 亲和串(HDU2203)

    http://acm.hdu.edu.cn/showproblem.php?pid=2203

    题目意思很简单,求s1串所构成的环中是否有s2这个串     

    用CMP参考http://s.acmore.net/show_article/show/44

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <iostream>
     5 #include <stack>
     6 #include <set>
     7 #include <queue>
     8 #define MAX(a,b) (a) > (b)? (a):(b)
     9 #define MIN(a,b) (a) < (b)? (a):(b)
    10 #define mem(a) memset(a,0,sizeof(a))
    11 #define INF 1000000007
    12 #define MAXN 100005
    13 using namespace std;
    14 
    15 char a[2*MAXN],b[MAXN];
    16 int next[MAXN];
    17 
    18 void get_next()
    19 {
    20     next[0]=-1;
    21     int key = -1;
    22     int index = 0;
    23     int len =strlen(b);
    24     while(index < len-1)
    25     {
    26         if(key == -1 || b[index] == b[key])
    27         {
    28             next[++index] = ++key;
    29         }
    30         else{
    31             key = next[key];
    32         }
    33     }
    34 }
    35 
    36 int KMP()
    37 {
    38     int A=0,B=0;
    39     int len1 = strlen(a);
    40     int len2 = strlen(b);
    41     while(A<len1 && B<len2)
    42     {
    43         if(B==-1 || a[A] == b[B])
    44         {
    45             A++;B++;
    46         }
    47         else
    48         {
    49             B=next[B];
    50         }
    51     }
    52     if(B >= len2)return 1;
    53     return 0;
    54 }
    55 
    56 int main()
    57 {
    58     while(~scanf("%s%s",a,b))
    59     {
    60         int len = strlen(a);
    61         for(int i=0; i<len; i++)
    62         {
    63             a[len+i] = a[i];
    64         }
    65         get_next();
    66         printf("%s
    ",KMP()?"yes":"no");
    67     }
    68     return 0;
    69 }
  • 相关阅读:
    出差常熟,郁闷中 沧海
    ABAP中程序之间传递参数的办法 沧海
    LSMW中出现No logical path specified. 沧海
    请认真对待生活 沧海
    escape sequence 沧海
    休假一周 沧海
    Print Program and Form Modify 沧海
    下周回南京 沧海
    LO020真麻烦 沧海
    函数讲解函数列表(ZT) 沧海
  • 原文地址:https://www.cnblogs.com/gj-Acit/p/3145037.html
Copyright © 2011-2022 走看看