Count the string (KMP+DP)

來源:互聯網
上載者:User

標籤:space   acm   題目   ace   pid   ext   etc   using   problem   

題目連結

 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4  5 inline int read() 6 { 7     int x=0,f=1;char ch=getchar(); 8     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 9     while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}10     return x*f;11 }12 13 /********************************************************************/14 15 const int maxn = 1e6+6;16 char s[maxn];17 int Next[maxn];18 int sum[maxn];19 20 const int mod = 10007;21 22 int main(){23     int t; t = read();24     while(t--){25         int n; n = read();26         scanf("%s", s+1);27         //int len = strlen(s+1);28         Next[1] = 0;;29         int k = 0;30         for(int i = 2;i <= n;i++){31             while(k > 0 && s[k+1] != s[i]){32                 k = Next[k];33             }34             if(s[k+1] == s[i])35                 k++;36             Next[i] = k;37         }38         int ans = 0;39         memset(sum, 0, sizeof(sum));40         for(int i = 1;i <= n;i++){41             sum[i] = (sum[Next[i]] + 1)%mod;42             ans = (ans + sum[i])%mod;43         }44         cout << ans << endl;45     }46     return 0;47 }

 

Count the string (KMP+DP)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.