HDU 1686 Oulipo

來源:互聯網
上載者:User

標籤:style   blog   color   os   io   for   

kmp

 

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5  6     int ans; 7     char n[1000010],m[10010]; 8     int next[10010]; 9 10 void getnext (char *s,int m,int *next){11     next[0]=next[1]=0;12     for (int i=1;i<m;i++){13         int j=next[i];14         while (j&&s[i]!=s[j])15             j=next[j];16         next[i+1]=s[i]==s[j]?j+1:0;17     }18 }19 20 int kmp (char *a,char *b,int n,int m,int *next){21     getnext (b,m,next);22     int j=0;23     int ans=0;24     for (int i=0;i<n;i++){25         while (j&&a[i]!=b[j])26             j=next[j];27         if (a[i]==b[j])28             j++;//cout<<j<<" ";29         if (j==m)30             ans++,j=next[j];31     }32     return ans;33 }34 35 int main (){36     int t;37     cin>>t;38     while (t--){39         scanf ("%s",m);40         scanf ("%s",n);41         ans=0;42         ans=kmp (n,m,strlen(n),strlen(m),next);43         printf ("%d\n",ans);44     }45     return 0;46 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.