1.Phone List(trie/easy)
//2015.10.26 09:55
1 //注意用静态建树,动态建树超时 2 #include <cstdio> 3 #include <iostream> 4 #include <string> 5 #include <vector> 6 #include <map> 7 #include <cstdlib> 8 #include <cstring> 9 10 using namespace std; 11 12 struct TrieNode 13 { 14 int next[10]; 15 bool flag; 16 bool ed; 17 TrieNode() 18 { 19 ed = false; 20 flag = false; 21 memset(next, 0, sizeof(next)); 22 } 23 }; 24 TrieNode trie[600050]; 25 int cnt = 1; 26 27 int main() 28 { 29 int t; 30 31 scanf("%d", &t); 32 while (t--) 33 { 34 cnt = 1; 35 memset(trie, 0, sizeof(trie)); 36 int n; 37 bool ans = true; 38 scanf("%d", &n); 39 char ch[15]; 40 memset(ch, 0, sizeof(ch)); 41 while (n--) 42 { 43 scanf("%s", ch); 44 int len = strlen(ch); 45 int tf = 0; 46 for (int i = 0; i < len; ++i) 47 { 48 if (trie[tf].next[ch[i]-'0'] == 0) 49 { 50 trie[tf].next[ch[i]-'0'] = cnt; 51 cnt++; 52 } 53 tf = trie[tf].next[ch[i]-'0']; 54 if (i == len-1 && trie[tf].flag) ans = false; 55 if (trie[tf].ed) ans = false; 56 trie[tf].flag = true; 57 } 58 trie[tf].ed = true; 59 } 60 puts(ans ? "YES" : "NO"); 61 } 62 63 return 0; 64 }
2.Constellations(hash/trie/kmp)
//2015.10.26 21:24
1 //把字符hash成long数 2 #include <cstdio> 3 #include <iostream> 4 #include <string> 5 #include <vector> 6 #include <map> 7 #include <cstdlib> 8 #include <cstring> 9 #include <set> 10 11 using namespace std; 12 13 int n, m, t, p, q; 14 char mp[1010][1010]; 15 long long h[1010][1010]; 16 char tmp[55]; 17 long long a[55]; 18 19 int ok() 20 { 21 for (int i = 0; i+p-1 < n; ++i) 22 { 23 for (int j = q-1; j < m; ++j) 24 { 25 int flag = 1; 26 for (int k = 0; k < p; ++k) 27 { 28 if (h[i+k][j] != a[k]) 29 { 30 flag = 0; 31 break; 32 } 33 } 34 if (flag) return true; 35 } 36 } 37 return false; 38 } 39 40 41 int main() 42 { 43 int ncas = 1; 44 while (~scanf("%d%d%d%d%d", &n, &m, &t, &p, &q)) 45 { 46 if (n+m+t+p+q == 0) break; 47 for (int i = 0; i < n; ++i) 48 scanf("%s", mp[i]); 49 memset(h, 0, sizeof(h)); 50 for (int i = 0; i < n; ++i) 51 for (int j = 0; j < q; ++j) 52 { 53 if (mp[i][j] == '*') h[i][q-1] |= (1LL << j); 54 } 55 for (int i = 0; i < n; ++i) 56 { 57 for (int j = q; j < m; ++j) 58 { 59 if (mp[i][j-q] == '*') h[i][j] = h[i][j-1]-1LL; 60 else h[i][j] = h[i][j-1]; 61 h[i][j] >>= 1LL; 62 if (mp[i][j] == '*') h[i][j] |= (1LL << (q-1)); 63 } 64 } 65 int cnt = 0; 66 while (t--) 67 { 68 for (int i = 0; i < p; ++i) 69 { 70 scanf("%s", tmp); 71 a[i] = 0; 72 for (int j = 0; j < q; ++j) 73 if (tmp[j] == '*') a[i] |= (1LL << j); 74 } 75 if (ok()) cnt++; 76 } 77 printf("Case %d: %d ", ncas++, cnt); 78 } 79 80 return 0; 81 }
用kmp总是TLE不知为啥,代码先贴到这
1 #include <cstdio> 2 #include <iostream> 3 #include <string> 4 #include <vector> 5 #include <map> 6 #include <cstdlib> 7 #include <cstring> 8 #include <set> 9 10 using namespace std; 11 12 int n, m, t, p, q; 13 14 char ch1[1000010]; 15 char ch2[50*55+10]; 16 int nxt[50*55+10]; 17 18 void getNext() 19 { 20 nxt[0] = -1; 21 int j = -1, len = strlen(ch2); 22 for (int i = 1; i < len; ++i) 23 { 24 while (j >= 0 && ch2[i] != ch2[j+1]) j = nxt[j]; 25 if (ch2[j+1] == ch2[i]) j++; 26 nxt[i] = j; 27 } 28 } 29 30 int judge() 31 { 32 int j = -1, len1 = strlen(ch1), len2 = strlen(ch2); 33 for (int i = 0; i < len1; ++i) 34 { 35 while (j >= 0 && ch2[j+1] != ch1[i] && ch2[j+1] != '#') j = nxt[j]; 36 if ((ch2[j+1] == ch1[i])) j++; 37 if (j+1 == len2) return 1; 38 if (ch2[j+1] == '#') j++, i += m-q; 39 } 40 return 0; 41 } 42 43 int main() 44 { 45 int ncas = 1; 46 while (~scanf("%d%d%d%d%d", &n, &m, &t, &p, &q)) 47 { 48 map<string, int> mp; 49 if (n+m+t+p+q == 0) break; 50 for (int i = 0; i < n; ++i) 51 scanf("%s", ch1+i*m); 52 int ans = 0; 53 while (t--) 54 { 55 for (int i = 0; i < p; ++i) 56 { 57 scanf("%s", ch2+i*(q+1)); 58 if (i != p-1) 59 ch2[(i+1)*(q+1)-1] = '#'; 60 } 61 if (mp.count(ch2) == 0) 62 { 63 getNext(); 64 int ret = judge(); 65 mp[ch2] = ret; 66 ans += ret; 67 } 68 else ans += mp[ch2]; 69 } 70 printf("Case %d: %d ", ncas++, ans); 71 } 72 73 return 0; 74 }
3.Long Long Message(后缀数组)
//2015.10.29 08:35
1 #include <cstdio> 2 #include <iostream> 3 #include <string> 4 #include <vector> 5 #include <map> 6 #include <cstdlib> 7 #include <cstring> 8 #include <set> 9 10 using namespace std; 11 12 const int maxn = 200010; 13 int dp[maxn][33]; 14 int wa[maxn], wb[maxn], wsf[maxn], wv[maxn], sa[maxn]; 15 int rank[maxn], height[maxn], s[maxn]; 16 char str[maxn], str1[maxn]; 17 18 int cmp(int *r, int a, int b, int k) 19 { 20 return r[a] == r[b] && r[a+k] == r[b+k]; 21 } 22 23 void getsa(int *r, int *sa, int n, int m) 24 { 25 int i, j, p, *x = wa, *y = wb, *t; 26 for (i = 0; i < m; ++i) wsf[i] = 0; 27 for (i = 0; i < n; ++i) wsf[x[i]=r[i]]++; 28 for (i = 1; i < m; ++i) wsf[i] += wsf[i-1]; 29 for (i = n-1; i >= 0; --i) sa[--wsf[x[i]]] = i; 30 p = j = 1; 31 for ( ; p < n; j *= 2, m = p) 32 { 33 for (p = 0, i = n-j; i < n; ++i) y[p++] = i; 34 for (i = 0; i < n; ++i) if (sa[i] >= j) y[p++] = sa[i]-j; 35 for (i = 0; i < n; ++i) wv[i] = x[y[i]]; 36 for (i = 0; i < m; ++i) wsf[i] = 0; 37 for (i = 0; i < n; ++i) wsf[wv[i]]++; 38 for (i = 1; i < m; ++i) wsf[i] += wsf[i-1]; 39 for (i = n-1; i >= 0; --i) sa[--wsf[wv[i]]] = y[i]; 40 t = x; 41 x = y; 42 y = t; 43 x[sa[0]] = 0; 44 for (p = 1, i = 1; i < n; ++i) 45 x[sa[i]] = cmp(y, sa[i-1], sa[i], j) ? p-1 : p++; 46 } 47 } 48 49 void getheight(int *r, int n) 50 { 51 int i, j, k = 0; 52 for (i = 1; i <= n; ++i) 53 rank[sa[i]] = i; 54 for (i = 0; i < n; ++i) 55 { 56 if (k) k--; 57 else k = 0; 58 j = sa[rank[i]-1]; 59 while (r[i+k] == r[j+k]) k++; 60 height[rank[i]] = k; 61 } 62 } 63 64 int main() 65 { 66 while (~scanf("%s", str)) 67 { 68 scanf("%s", str1); 69 int n = 0, len = strlen(str); 70 for (int i = 0; i < len; ++i) 71 s[n++] = str[i]-'a'+1; 72 s[n++] = 28; 73 len = strlen(str1); 74 for (int i = 0; i < len; ++i) 75 s[n++] = str1[i]-'a'+1; 76 s[n] = 0; 77 getsa(s, sa, n+1, 30); 78 getheight(s, n); 79 int maxx = 0, pos = 0; 80 len = strlen(str); 81 for (int i = 2; i < n; ++i) 82 if (height[i] > maxx) 83 { 84 if (0<=sa[i-1] && sa[i-1]<len && len<sa[i]) 85 maxx = height[i]; 86 if (0<=sa[i] && sa[i]<len && len<sa[i-1]) 87 maxx = height[i]; 88 } 89 printf("%d ", maxx); 90 } 91 92 return 0; 93 }