POJ 1226 后缀数组 或 KMP 或 暴力
2017-09-13 10:00
441 查看
简略题意:出现或反转后出现在每个字符串中的最长字符串。
先将每个串和自己的反转串连接起来,随后将这若干个串连接起来。二分答案,判定就分组看是否有一组后缀在所有原串或者原串的反转串中出现。
先将每个串和自己的反转串连接起来,随后将这若干个串连接起来。二分答案,判定就分组看是否有一组后缀在所有原串或者原串的反转串中出现。
#include <iostream> #include <cstring> #include <map> #include <cstdio> #include <vector> #include <algorithm> using namespace std; const int N = 1e5+1100; int n, q; int str ; char str2 ; int belong ; int f = 0; int minv; namespace SA { int sa , rank , height , s[N<<1], t[N<<1], p , cnt , cur ; #define pushS(x) sa[cur[s[x]]--] = x #define pushL(x) sa[cur[s[x]]++] = x #define inducedSort(v) fill_n(sa, n, -1); fill_n(cnt, m, 0); \ for (int i = 0; i < n; i++) cnt[s[i]]++; \ for (int i = 1; i < m; i++) cnt[i] += cnt[i-1]; \ for (int i = 0; i < m; i++) cur[i] = cnt[i]-1; \ for (int i = n1-1; ~i; i--) pushS(v[i]); \ for (int i = 1; i < m; i++) cur[i] = cnt[i-1]; \ for (int i = 0; i < n; i++) if (sa[i] > 0 && t[sa[i]-1]) pushL(sa[i]-1); \ for (int i = 0; i < m; i++) cur[i] = cnt[i]-1; \ for (int i = n-1; ~i; i--) if (sa[i] > 0 && !t[sa[i]-1]) pushS(sa[i]-1) void sais(int n, int m, int *s, int *t, int *p) { int n1 = t[n-1] = 0, ch = rank[0] = -1, *s1 = s+n; for (int i = n-2; ~i; i--) t[i] = s[i] == s[i+1] ? t[i+1] : s[i] > s[i+1]; for (int i = 1; i < n; i++) rank[i] = t[i-1] && !t[i] ? (p[n1] = i, n1++) : -1; inducedSort(p); for (int i = 0, x, y; i < n; i++) if (~(x = rank[sa[i]])) { if (ch < 1 || p[x+1] - p[x] != p[y+1] - p[y]) ch++; else for (int j = p[x], k = p[y]; j <= p[x+1]; j++, k++) if ((s[j]<<1|t[j]) != (s[k]<<1|t[k])) {ch++; break;} s1[y = x] = ch; } if (ch+1 < n1) sais(n1, ch+1, s1, t+n, p+n1); else for (int i = 0; i < n1; i++) sa[s1[i]] = i; for (int i = 0; i < n1; i++) s1[i] = p[sa[i]]; inducedSort(s1); } template<typename T> int mapCharToInt(int n, const T *str) { int m = *max_element(str, str+n); fill_n(rank, m+1, 0); for (int i = 0; i < n; i++) rank[str[i]] = 1; for (int i = 0; i < m; i++) rank[i+1] += rank[i]; for (int i = 0; i < n; i++) s[i] = rank[str[i]] - 1; return rank[m]; } template<typename T> void suffixArray(int n, const T *str) { int m = mapCharToInt(++n, str); sais(n, m, s, t, p); for (int i = 0; i < n; i++) rank[sa[i]] = i; for (int i = 0, h = height[0] = 0; i < n-1; i++) { int j = sa[rank[i]-1]; while (i+h < n && j+h < n && s[i+h] == s[j+h]) h++; if (height[rank[i]] = h) h--; } } template <typename T> void init(T *str){ str = 0; suffixArray(n, str); } int vis[110], count = 0; void checkinit() { memset(vis, 0, sizeof vis); count = 0; } bool check(int x) { checkinit(); for(int i = 1; i <= n; i++) { if(height[i] >= x) { int bl = belong[sa[i]]; if(!vis[bl]) vis[bl] = 1, count ++; if(count == q) return 1; } else { checkinit(); int bl = belong[sa[i]]; if(!vis[bl]) vis[bl] = 1, count ++; } } return 0; } void solve() { int l = 0, r = minv + 1, m; while(l < r) { m = (l + r) >> 1; if(check(m)) l = m + 1; else r = m; } printf("%d\n", l - 1); } }; int t; int main() { scanf("%d", &t); while(t--) { scanf("%d", &q); int s = 0; int d = 128; minv = 0x3f3f3f3f; for(int i = 1; i <= q; i++) { scanf("%s", str2); int len = strlen(str2); for(int j = 0; j < len; j++) str[s+j] = str2[j], belong[s+j] = i; s += len; minv = min(minv, len); str[s] = d; d++; s++; for(int j = len - 1; j >= 0; j--) str[s+len-1-j] = str2[j], belong[s+j] = i; s += len; minv = min(minv, len); str[s] = d; d++; s++; } n = s; SA::init(str); SA::solve(); } return 0; }
相关文章推荐
- poj 1226 出现或反转后出现在每个字符串中的最长子串(后缀数组/kmp)
- poj 1226 求多串最长公共子串 或 回文子串 KMP&&strstr&&后缀数组
- |poj 1226|后缀数组|二分|Substrings
- POJ 1226后缀数组:求出现或反转后出现在每个字符串中的最长子串
- POJ 2752+KMP+利用next数组性质求出所有相同的前缀和后缀
- POJ 2406 连续重复字符串(KMP)和后缀数组
- POJ 2406 后缀数组 或 KMP
- POJ 2406 Power Strings 【后缀数组||KMP】
- POJ 1226 Substrings KMP暴力 或 后缀数组
- poj1226 Substrings (应该用kmp,但是数据弱,暴力枚举就行了)
- poj 2406 连续重复子串(后缀数组DC3/kmp)
- POJ1226---Substrings(后缀数组+二分)
- POJ 1226 Substrings (后缀数组 n个串的最长公共子串)
- POJ 1226 Substrings 后缀数组
- poj 2406 Power Strings (后缀数组 || KMP)
- POJ3080方法很多(暴力,KMP,后缀数组,DP)
- POJ 1226 Substrings(后缀数组+二分答案)
- 【POJ 1226】Substrings【后缀数组】
- POJ - 2752 Seek the Name, Seek the Fame(KMP中对next数组的前缀后缀匹配应用)
- POJ 3693 Maximum repetition substring 后缀数组 暴力 rmq