Test instructions: Given n strings, let you find out the maximum R, so that there is a SL is not a sub-string of SR (L < R).
Analysis: KMP algorithm, but the direct violence don't think about, definitely tle, so we think about, with two pointers L, R, if SL is not a string of SR, then we can update R, continue back, until the last found.
The code is as follows:
#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <cstdio> #include <string> #include < cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include < queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include < cmath> #include <stack> #define DEBUG puts ("+++++")//#include <tr1/unordered_map> #define Freopenr Freopen ("In.txt", "R", stdin) #define FREOPENW freopen ("OUT.txt", "w", stdout) using namespace std;//using namespace std:: Tr1;typedef Long Long ll;typedef pair<int, int> p;const int inf = 0x3f3f3f3f;const double inf = 0x3f3f3f3f3f3f;cons T LL LNF = 0x3f3f3f3f3f3f;const Double PI = ACOs ( -1.0); const double EPS = 1e-8;const int maxn = 2e3 + 5;const LL mod = 214 7493647;const int N = 1e6 + 5;const int dr[] = {-1, 0, 1, 0, 1, 1,-1, -1};const int dc[] = {0, 1, 0,-1, 1,-1, 1, -1};co NST char *hex[] = {"0000", "0001", "0010", "0011", "0100","0101", "0110", "0111", "$", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};inline ll gcd (ll A, ll b) {return b = = 0? A:GCD (b, a%b); }inline int gcd (int a, int b) {return b = = 0? a:gcd (b, a%b);} inline int LCM (int a, int b) {return a * B/GCD (a, b);} int n, m;const int mon[] = {0, 31, 29, 31, 30, 31, 0, +, +,, +, +, +,, N, H, H, C, h, 31};inline int Min (int a, int b) {return a < b? A:b;} inline int Max (int a, int b) {return a > b a:b;} inline ll Min (ll A, ll b) {return a < b a:b;} inline ll Max (ll A, ll b) {return a > b a:b;} inline bool Is_in (int r, int c) {return R >= 0 && r < n && C >= 0 && C < m;} int F[maxn];char s[505][maxn];void getfail (char *p) {int m = strlen (P); F[0] = f[1] = 0; for (int i = 1; i < m; ++i) {int j = f[i]; while (J && p[i]! = p[j]) j = f[j]; F[I+1] = p[i] = = P[j]? j+1:0;}}bool Match (Char *t, char *p) {int n = strlen (T), M = strlen (P); Getfail (P); int j = 0; for (int i = 0, i < n; ++i) {while (J && p[j]! = t[i]) j = f[j]; if (p[j] = = T[i]) ++j; if (j = = m) return true; } return false;} int main () {int T; Cin >> T; for (int kase = 1; kase <= T; ++kase) {scanf ("%d", &n); int ans =-1; int L = 1, r = 2; for (int i = 1; I <= n; ++i) scanf ("%s", s+i); while (r <= N) {while (L < R) {if (Match (S[r], s[l])) ++l; else {ans = R; Break }} ++r; } printf ("Case #%d:%d\n", Kase, ans); } return 0;}
HDU 5510 Bazinga (KMP)