[HDU3336]KMP (suffix array)

Source: Internet
Author: User
Tags gcd

Test instructions: The sum of the occurrences of all prefixes of the string s.

Http://www.cnblogs.com/jklongint/p/4446117.html

Idea: Use KMP to do, simple and high efficiency. The prefix end of the position classification, so that Dp[i] is at the end position in the number of prefixes in I, then dp[i] = CNT (j) (J~i is the prefix), and by the KMP of the next function of the Transfer property, the following recursive equation can be obtained: dp[i] = Dp[next[i]] + 1, the recursive expansion, that is, i = next[i] constant iteration, then +1 is the number of dp[i] = CNT (j) (J~i is the prefix) of the CNT (j). This is the nature of the next array, which is really ingenious!

1 #pragmaComment (linker, "/stack:10240000,10240000")2 3#include <iostream>4#include <cstdio>5#include <algorithm>6#include <cstdlib>7#include <cstring>8#include <map>9#include <queue>Ten#include <deque> One#include <cmath> A#include <vector> -#include <ctime> -#include <cctype> the#include <Set> -#include <bitset> -#include <functional> -#include <numeric> +#include <stdexcept> -#include <utility> +  A using namespacestd; at  - #defineMem0 (a) memset (a, 0, sizeof (a)) - #defineMem_1 (a) memset (a,-1, sizeof (a)) - #defineLson L, M, RT << 1 - #defineRson m + 1, R, RT << 1 | 1 - #definedefine_m int m = (L + r) >> 1 in #defineRep_up0 (A, b) for (int a = 0; a < (b); a++) - #defineRep_up1 (A, b) for (int a = 1; a <= (b); a++) to #defineRep_down0 (A, b) for (int a = b-1; a >= 0; a--) + #defineRep_down1 (A, b) for (int a = b; a > 0; a--) - #defineAll (a) (a). Begin (), (a). End () the #defineLowbit (x) ((x) & (-(x))) * #defineCONSTRUCTINT4 (name, a, B, C, D) name (int a = 0, int b = 0, int c = 0, int d = 0): A (a), B (b), C (c), D (d) {} $ #defineCONSTRUCTINT3 (name, a, B, c) name (int a = 0, int b = 0, int c = 0): A (a), B (b), C (c) {}Panax Notoginseng #defineConstructInt2 (name, a, b) name (int a = 0, int b = 0): A (a), B (b) {} - #definePCHR (a) Putchar (a) the #definePstr (a) printf ("%s", a) + #defineSstr (a) scanf ("%s", a) A #defineSint (a) scanf ("%d", &a) the #defineSint2 (A, b) scanf ("%d%d", &a, &b) + #defineSint3 (A, B, c) scanf ("%d%d%d", &a, &b, &c) - #definePint (a) printf ("%d\n", a) $ #defineTest_print1 (a) cout << "var1 =" << a << Endl $ #defineTest_print2 (A, b) cout << "var1 =" << a << ", var2 =" << b << Endl - #defineTest_print3 (A, B, c) cout << "var1 =" << a << ", var2 =" << b << ", Var3 =" << c &L t;< Endl -  thetypedefDoubledb; -typedefLong LongLL;Wuyitypedef pair<int,int>PII; thetypedef multiset<int>MSI; -typedefSet<int>si; Wutypedef vector<int>VI; -typedef map<int,int>Mii; About  $ Const intdx[8] = {0,0, -1,1,1,1, -1, -1}; - Const intdy[8] = {-1,1,0,0,1, -1,1, -1 }; - Const intMAXN = 2e5 +7; - Const intMD =10007; A Const intINF = 1e9 +7; + ConstLL inf_l = 1e18 +7; the Const DoublePI = ACOs (-1.0); - Const DoubleEPS = 1e-6; $  thetemplate<classT>t gcd (t A, T b) {returnb==0? A:GCD (b,a%b);} thetemplate<classT>BOOLMax_update (T &a,ConstT &b) {if(B>a) {a = B;return true;}return false;} thetemplate<classT>BOOLMin_update (T &a,ConstT &b) {if(B<a) {a = B;return true;}return false;} thetemplate<classT>t condition (BOOLF, t A, T b) {returnF?a:b;} -template<classT>voidCopy_arr (t a[], T b[],intN) {rep_up0 (i,n) a[i]=b[i];} in intMAKE_ID (intXintYintN) {returnX * n +y;} the  the structKMP { About     intnext[1000010]; the     voidGetNext (Chars[]) { the mem0 (next); thenext[0] = next[1] =0; +          for(inti =1; S[i]; i++) { -             intj =Next[i]; the              while(j && s[i]! = s[j]) j =Next[j];BayiNext[i +1] = s[j] = = S[i]? J +1:0; the         } the     } - }; -  the KMP KMP; the intDP[MAXN]; the CharS[MAXN]; the  - intMain () { the     //freopen ("In.txt", "R", stdin); the     intT, N; theCIN >>T;94      while(t--) { thescanf"%d%s", &N, s); the KMP. GetNext (s); the MEM0 (DP);98         intAns =N; About Rep_up1 (i, n) { -             if(Kmp.next[i] = =0)Continue;101Dp[i] = Dp[kmp.next[i]] +1;102Dp[i]%=MD;103Ans + =Dp[i];104Ans%=MD; the         }106cout << ans <<Endl;107     }108     return 0;109}
View Code

[hdu3336]kmp (suffix array)

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.