Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 5349
I did not read this question during the competition. Unfortunately, it is not difficult to think of algorithms, but I did not write code twice. I still read other people's code and wrote this question.
Learned:
1. The first table in the hash string is empty and easy to write.
2. All hash matching conditions are matched starting from all the locations of the text string. If the text is smaller than the patern and the text can be cyclically determined
3. to speed up DFS, You can estimate the minimum matching length.
Int L = min (Len, N + 1-st); this saves a lot of time
4. pre-process hash B to the power and hash of the string.
// # Pragma comment (linker, "/Stack: 102400000,102400000 ") # include <cstdio> # include <cstring> # include <algorithm> # include <string> # include <iostream> # include <iomanip> # include <cmath> # include <map> # include <set> # include <queue> using namespace STD; # define ls (RT) rt * 2 # define RS (RT) rt * 2 + 1 # define ll long # define ull unsigned long # define rep (I, S, e) for (INT I = s; I <E; I ++) # define repe (I, S, e) for (int I = s; I <= E; I ++) # define Cl (a, B) memset (a, B, sizeof (A) # define in (s) freopen (S, "r", stdin) # define out (s) freopen (S, "W", stdout) const ll ll_inf = (ull) (-1)> 1; const double EPS = 1e-8; const int INF = 100000000; const ull B = 1e8 + 7; const int maxn = 100000 + 5; vector <pair <int, int> ans; char STR [maxn]; ull ha [9] [maxn], TT [maxn], Hb [maxn]; // int leg [10], lenp; int n, m; void calhb () {HB [0] = 0; For (INT I = 1; I <= m; I ++) Hb [I] = HB [I-1] * B + STR [I];} void read () {ans. clear (); scanf ("% d", & N, & M); For (INT I = 0; I <8; I ++) {scanf ("% s", STR + 1); int Len = strlen (STR + 1); // leg [I] = Len; ha [I] [0] = 0; For (Int J = 1; j <= Len; j ++) ha [I] [J] = ha [I] [J-1] * B + STR [J]; I ++; reverse (STR + 1, STR + 1 + strlen (STR + 1); For (Int J = 1; j <= Len; j ++) ha [I] [J] = ha [I] [J-1] * B + STR [J];} scanf ("% s", STR + 1 ); calhb ();} int DFS (int K, int St, int Len) {// printf ("# K = % D ST = % d Len = % d \ n ", K, St, Len ); /// // If (LEN = 0) return 1; int L = min (Len, N + 1-st ); if (ha [k] [st + L-1]-ha [k] [ST-1] * TT [l] = HB [M-len + L]-Hb [M-Len] * TT [l]) {ans. push_back (make_pair (K, St); Len-= L; int BAC = K % 2? K-1: k + 1; if (LEN = 0) return 1; for (INT I = 0; I <8; I ++) if (I! = BAC & DFS (I, 1, Len) return 1; ans. pop_back () ;}return 0 ;}int solve () {for (INT I = 0; I <8; I ++) for (Int J = 1; j <= N; j ++) {ans. clear (); If (DFS (I, j, m) return 1;} return 0;} vector <int> op; void out () {op. clear (); int CNT = 0; For (INT I = 0; I <ans. size (); I ++) {// printf ("# K = % d ST = % d \ n", ANS [I]. first, ANS [I]. second); If (ANS [I]. first % 2 = 0) // forward {for (Int J = ans [I]. second; j <= N & CNT <m; j ++) {op. push_back (ANS [I ]. First/2 * n + J); CNT ++ ;}} else {for (Int J = ans [I]. second; j <= N & CNT <m; j ++) {op. push_back (ANS [I]. first/2 * n + N-J + 1); CNT ++ ;}} printf ("% d", op [0]); For (INT I = 1; I <op. size (); I ++) printf ("% d", op [I]); putchar ('\ n');} int main () {// In ("zoj3810.txt"); int ncase; scanf ("% d", & ncase); TT [0] = 1; for (INT I = 1; I <maxn; I ++) TT [I] = TT [I-1] * B; while (ncase --) {read (); If (solve () Out (); else puts ("no solution! ");} Return 0 ;}
Zoj 3817 2014 Mudanjiang online competition string hash