POJ 3729 Facer’s string

来源:互联网 发布:百度人工智能产品 编辑:程序博客网 时间:2024/05/22 18:31
#include <iostream>#include <queue> #include <stack> #include <map> #include <set> #include <bitset> #include <cstdio> #include <algorithm> #include <cstring> #include <climits>#include <cstdlib>#include <cmath>#include <time.h>#define maxn 100005#define maxm 400005#define eps 1e-10#define mod 1000000007#define INF 0x3f3f3f3f#define PI (acos(-1.0))#define lowbit(x) (x&(-x))#define mp make_pair#define ls o<<1#define rs o<<1 | 1#define lson o<<1, L, mid #define rson o<<1 | 1, mid+1, R//#pragma comment(linker, "/STACK:16777216")typedef long long LL;typedef unsigned long long ULL;//typedef int LL;using namespace std;LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}// headint c[maxn];int s[maxn], t1[maxn], t2[maxn], sa[maxn];int rank[maxn], height[maxn];void build(int n, int m){int *x = t1, *y = t2;for(int i = 0; i < m; i++) c[i] = 0;for(int i = 0; i < n; i++) c[x[i] = s[i]]++;for(int i = 1; i < m; i++) c[i] += c[i-1];for(int i = n-1; i >= 0; i--) sa[--c[x[i]]] = i;for(int k = 1; k <= n; k <<= 1) {int p = 0;for(int i = n - k; i < n; i++) y[p++] = i;for(int i = 0; i < n; i++) if(sa[i] >= k) y[p++] = sa[i] - k;for(int i = 0; i < m; i++) c[i] = 0;for(int i = 0; i < n; i++) c[x[y[i]]]++;for(int i = 1; i < m; i++) c[i] += c[i-1];for(int i = n-1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];p = 1, swap(x, y), x[sa[0]] = 0;for(int i = 1; i < n; i++)x[sa[i]] = y[sa[i]] == y[sa[i-1]] && y[sa[i]+k] == y[sa[i-1]+k] ? p-1 : p++;if(p >= n) break;m = p;}}void getheight(int n){int k = 0;for(int i = 0; i <= n; i++) rank[sa[i]] = i;for(int i = 0; i < n; i++) {if(k) k--;int j = sa[rank[i] - 1];while(s[i+k] == s[j+k]) k++;height[rank[i]] = k;}}int n, m, k, len;void read(void){for(int i = 0; i < n; i++) scanf("%d", &s[i]), s[i] += 2;s[n] = 0;for(int i = n+1; i <= n + m; i++) scanf("%d", &s[i]), s[i] += 2;len = n + m + 1;s[len] = 1;}void work(void){build(len + 1, 10005);getheight(len);int t1 = 0, t2 = 0, flag = 0;int ans = 0, res = 0;for(int i = 1; i <= len; i++) {if(height[i] < k) {if(t2) ans += t1;t1 = t2 = flag = 0;}else {if(!flag) {if(sa[i-1] < n) t1++;else t2++;flag = 1;}if(sa[i] < n) t1++;else t2++;}}if(t2) ans += t1;t1 = t2 = flag = 0;for(int i = 1; i <= len; i++) {if(height[i] < k + 1) {if(t2) res += t1;t1 = t2 = flag = 0;}else {if(!flag) {if(sa[i-1] < n) t1++;else t2++;flag = 1;}if(sa[i] < n) t1++;else t2++;}}if(t2) res += t1;printf("%d\n", ans - res);}int main(void){while(scanf("%d%d%d", &n, &m, &k)!=EOF) {read();work();}return 0;}

0 0