Hdu3336 (Count the string) KMP Application

Source: Internet
Author: User

Give a string and calculate the number of times all prefixes appear in the string.


Solution: After KMP calculates the Next array, the Next array at each position is continuously recursive forward, and 1 is added for each prefix.


Code:

/******************************************************* author:xiefubao*******************************************************/#pragma comment(linker, "/STACK:102400000,102400000")#include 
 
  #include 
  
   #include 
   
    #include 
    
     #include 
     
      #include 
      
       #include #include 
       
        #include 
        #include 
         
          #include 
          
           #include 
           
            //freopen ("in.txt" , "r" , stdin);using namespace std;#define eps 1e-8const double pi=acos(-1.0);typedef long long LL;const int Max=200100;const int INF=10007;int Next[Max];int num[Max];char s[Max];int len=0;void get_next(){ int i=0; int j=Next[0]=-1; while(i
            
             >t; while(t--) { //memset(num,0,sizeof num); scanf("%d",&len); scanf("%s",s); get_next(); for(int i=0;i<=len;i++) { num[i]=1; int t=Next[i]; while(t>0) { num[t-1]=(num[t-1]+1)%INF; t=Next[t]; } } int ans=0; for(int i=0;i
             
              

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.