Question: calculate the total number of occurrences of each prefix in the parent string. AC code:
# Include <iostream> # include <cstdio> # include <cstdlib> # include <cmath> # include <cstring> # include <string> # include <vector> # include <list> # include <deque> # include <queue> # include <iterator> # include <stack> # include <map> # include <set> # include <algorithm> # include <cctype> using namespace std; typedef long LL; const int N = 200005; const ll ii = 100000000; const int INF = 0x3f3f3f; cons T double PI = acos (-1.0); int next [N], c [N], ans, len; char str [N]; void getNext (char * p) {memset (c, 0, sizeof (c); int j = 0, k =-1; next [0] =-1; ans = 0; while (j <len) // len is the length of p {if (k =-1 | p [j] = p [k]) {if (k! =-1) {c [j] = c [k] + 1; ans + = c [j];} j ++; k ++; next [j] = k;} else k = next [k];} int main () {int I, j, T; cin> T; while (T --) {scanf ("% d % s", & len, str); getNext (str); printf ("% d \ n", (ans + len) % 10007 );} return 0 ;} # include <iostream> # include <cstdio> # include <cstdlib> # include <cmath> # include <cstring> # include <string> # include <vector> # include <list> # include <deque> # include <queue> # include <iterator> # include <Stack> # include <map> # include <set> # include <algorithm> # include <cctype> using namespace std; typedef long LL; const int N = 200005; const ll ii = 100000000; const int INF = 0x3f3f3f3f; const double PI = acos (-1.0); int next [N], c [N], ans, len; char str [N]; void getNext (char * p) {memset (c, 0, sizeof (c); int j = 0, k =-1; next [0] =-1; ans = 0; while (j <len) // len is the length of p {if (k =-1 | p [j] = p [k]) {if (k! =-1) {c [j] = c [k] + 1; ans + = c [j];} j ++; k ++; next [j] = k;} else k = next [k];} int main () {int I, j, T; cin> T; while (T --) {scanf ("% d % s", & len, str); getNext (str); printf ("% d \ n", (ans + len) % 10007 );} return 0 ;}