I wanted to do the number theory ... But all the other dalao are making the leap.
So......
Chapter I KMP
KMP, the most critical one is not this semi-violent single-mode match.
But this NXT array often has some strange problems, especially the Loop section can be obtained directly from t-nxt[t] ... Oh, God.
In short, remember that the NXT is the longest public prefix in the suffix of the tail pointer is OK
T1 poj3461 Oulipo
Time Cost:10min
The pure board really doesn't have anything to say.
Is that every time you empty the NXT, OK
Code:
1#include <cstdio>2#include <cstring>3 using namespacestd;4 Const intN =1000005;5 #defineRep (i,a,n) for (int i = a;i <= n;i++)6 #defineMS (A, b) memset (a,b,sizeof a)7 8 CharS[n],t[n];9 ints,t;Ten intNxt[n]; One intans; A voidCLR () {MS (NXT,0), ans =0;} - voidgnxt () { - intTMP =0; theRep (I,2, T) { - while(TMP && t[tmp+1] = T[i]) tmp =nxt[tmp]; - if(t[tmp+1] = = T[i]) Nxt[i] = + +tmp; - } + } - voidKMP () { + intTMP =0; ARep (I,1, S) { at while(TMP && t[tmp+1] = S[i]) tmp =nxt[tmp]; - if(t[tmp+1] ==S[i]) { -tmp++; - if(tmp = = T) ans++; - } - } in } - to + intMain () { - intQ; thescanf"%d",&q); * while(q--) { $ CLR ();Panax Notoginsengscanf"%s", t+1), scanf ("%s", s+1); -S = strlen (s+1), T = strlen (t+1); the gnxt (); + KMP (); Aprintf"%d\n", ans); the } +}
View Code
T2 POJ 2406 Power strings
Time Cost:55min
Have you ever met before? The last violent roll of rough
Today, I understand it by playing the table .
As the NXT is defined, the string itself is the longest common prefix of its own
In the last time we asked for NXT, we considered the part that was nxt[t] discarded (recorded as Len)
If this segment is divisible by T, consider the second Len long string
It is equal to the first Len long string.
The same can be pushed to T/len times
At the same time NXT take maximum goo t-nxt[t] take the smallest
So if T is an integral multiple of t-nxt[t] then t-nxt[t] is the smallest cyclic section
Code:
1#include <cstdio>2#include <cstring>3 using namespacestd;4 Const intN =1000005;5 #defineRep (i,a,n) for (int i = a;i <= n;i++)6 #defineMS (A, b) memset (a,b,sizeof a)7 8 CharS[n],t[n];9 ints,t;Ten intNxt[n]; One intans; A voidCLR () {MS (NXT,0), ans =0;} - voidgnxt () { - intTMP =0; theRep (I,2, T) { - while(TMP && t[tmp+1] = T[i]) tmp =nxt[tmp]; - if(t[tmp+1] = = T[i]) Nxt[i] = + +tmp; - } + } - voidKMP () { + intTMP =0; ARep (I,1, S) { at while(TMP && t[tmp+1] = S[i]) tmp =nxt[tmp]; - if(t[tmp+1] ==S[i]) { -tmp++; - if(tmp = = T) ans++; - } - } in } - to + intMain () { - while(1) { the CLR (); *scanf"%s", t+1); $T = strlen (t+1);Panax Notoginseng if(T = =1&& t[1] =='.')return 0; - gnxt (); the if(T% (t-nxt[t]) = =0) printf ("%d\n", t/(tnxt[t])); + ElsePuts"1"); A } the}
View Code
T3 cf562d Om Nom and necklace
Time Cost:45min
It's easier to think of a previous problem.
Cross can't do we see AB as the cycle section
Because K is given, it is possible to find a cyclic section length range by I and K
Ok if this length is an integer multiple of the shortest loop section obtained by NXT
The time to achieve this is to save Len and then determine if the legal interval exists so that you can automatically subtract the remainder of a
However, it is important to note that a or B is empty and is divided into K or k+1 segments (without suffixes) to be
(The actual A is empty already processed)
Code:
#include <cstdio>#include<cstring>using namespacestd;Const intN =1000005;#defineRep (i,a,n) for (int i = a;i <= n;i++)#defineMS (A, b) memset (a,b,sizeof a)CharS[n],t[n];ints,t;intNxt[n];intans;voidCLR () {MS (NXT,0), ans =0;}voidgnxt () {intTMP =0; Rep (I,2, T) { while(TMP && t[tmp+1] = T[i]) tmp =Nxt[tmp]; if(t[tmp+1] = = T[i]) Nxt[i] = + +tmp; }}voidKMP () {intTMP =0; Rep (I,1, S) { while(TMP && t[tmp+1] = S[i]) tmp =Nxt[tmp]; if(t[tmp+1] ==S[i]) {tmp++; if(tmp = = T) ans++; } }}intn,k;intMain () {scanf ("%d%d",&n,&k); scanf ("%s", t+1); T=N; GNXT (); Rep (I,1, T) { intLen = i-Nxt[i]; if(i% (k +1) ==0&& (I/(k +1))% Len = =0) Putchar ('1');//lenb==0 Else { //Circular subsequence Range intUPP = I/k,down = I/(k +1) +1; UPP= UPP/Len; down= (down + len-1) /Len; if(Upp >= down) putchar ('1'); ElsePutchar ('0'); }} puts (""); return 0;}
View Code
To be Continued ...
Practice II String