trie树。以puzzle做trie树内存不够,从puzzle中直接找串应该会TLE。其实可以将查询组成trie树,离线做。
扫描puzzle时注意仅三个方向即可。
1 /* 1857 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #include <vector> 9 #include <deque> 10 #include <algorithm> 11 #include <cstdio> 12 #include <cmath> 13 #include <ctime> 14 #include <cstring> 15 #include <climits> 16 #include <cctype> 17 #include <cassert> 18 #include <functional> 19 #include <iterator> 20 #include <iomanip> 21 using namespace std; 22 //#pragma comment(linker,"/STACK:102400000,1024000") 23 24 #define sti set<int> 25 #define stpii set<pair<int, int> > 26 #define mpii map<int,int> 27 #define vi vector<int> 28 #define pii pair<int,int> 29 #define vpii vector<pair<int,int> > 30 #define rep(i, a, n) for (int i=a;i<n;++i) 31 #define per(i, a, n) for (int i=n-1;i>=a;--i) 32 #define clr clear 33 #define pb push_back 34 #define mp make_pair 35 #define fir first 36 #define sec second 37 #define all(x) (x).begin(),(x).end() 38 #define SZ(x) ((int)(x).size()) 39 #define lson l, mid, rt<<1 40 #define rson mid+1, r, rt<<1|1 41 42 const int maxr = 505; 43 const int maxc = 505; 44 const int maxn = 2e5+5; 45 const int maxm = 1e4+5; 46 char s[maxr][maxc], ss[maxc]; 47 int X[maxn], Y[maxn]; 48 int nxt[maxn][26]; 49 int P[maxm]; 50 int l = 1; 51 int wn = 0; 52 int n, m; 53 54 void init() { 55 memset(X, -1, sizeof(X)); 56 memset(Y, -1, sizeof(Y)); 57 } 58 59 inline int newNode() { 60 return l++; 61 } 62 63 void Insert(char *s) { 64 int i = 0, id; 65 int p = 0, q; 66 67 while (s[i]) { 68 id = s[i] - 'A'; 69 q = nxt[p][id]; 70 if (!q) 71 q = nxt[p][id] = newNode(); 72 p = q; 73 ++i; 74 } 75 P[wn++] = p; 76 } 77 78 void Find(char *s, int x, int y) { 79 int i = 0, id; 80 int p = 0; 81 82 while (s[i]) { 83 id = s[i] - 'A'; 84 p = nxt[p][id]; 85 if (!p) 86 return ; 87 if (X[p]<0 || x<X[p] || (x==X[p] && y<Y[p])) { 88 X[p] = x; 89 Y[p] = y; 90 } 91 ++i; 92 } 93 } 94 95 void solve() { 96 // horizontal 97 rep(i, 0, n) { 98 strcpy(ss, s[i]); 99 rep(j, 0, m) 100 Find(ss+j, i, j); 101 } 102 103 104 // vertical 105 rep(j, 0, m) { 106 rep(i, 0, n) 107 ss[i] = s[i][j]; 108 ss[n] = '