Common Substrings POJ - 3415(长度不小于k的公共子串的个数)

题意:

  给定两个字符串A 和 B, 求长度不小于 k 的公共子串的个数(可以相同)

Common Substrings POJ - 3415(长度不小于k的公共子串的个数)

分两部分求和sa[i-1] > len1  sa[i] < len1  和  sa[i-1] < len1   sa[i] > len1

Common Substrings POJ - 3415(长度不小于k的公共子串的个数)

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
int s[maxn];
int sa[maxn], t[maxn], t2[maxn], c[maxn], n;
int ran[maxn], height[maxn]; void get_sa(int m)
{
int i, *x = t, *y = t2;
for(i = ; i < m; i++) c[i] = ;
for(i = ; i < n; i++) c[x[i] = s[i]]++;
for(i = ; i < m; i++) c[i] += c[i-];
for(i = n-; i >= ; i--) sa[--c[x[i]]] = i;
for(int k = ; k <= n; k <<= )
{
int p = ;
for(i = n-k; i < n; i++) y[p++] = i;
for(i = ; i < n; i++) if(sa[i] >= k) y[p++] = sa[i] - k;
for(i = ; i < m; i++) c[i] = ;
for(i = ; i < n; i++) c[x[y[i]]]++;
for(i = ; i< m; i++) c[i] += c[i-];
for(i = n-; i >= ; i--) sa[--c[x[y[i]]]] = y[i];
swap(x, y);
p = ; x[sa[]] = ;
for(i = ; i < n; i++)
x[sa[i]] = y[sa[i-]] == y[sa[i]] && y[sa[i-]+k] == y[sa[i]+k] ? p- : p++;
if(p >= n) break;
m = p;
}
int k = ;
for(i = ; i < n; i++) ran[sa[i]] = i;
for(i = ; i < n; i++)
{
if(k) k--;
int j = sa[ran[i]-];
while(s[i+k] == s[j+k]) k++;
height[ran[i]] = k;
}
} int k, top, num;
LL sum, ans;
char s1[maxn], s2[maxn];
int stac[maxn], cnt[maxn];
int main()
{
while(~rd(k) && k)
{
top = sum = num = ans = n = ;
rs(s1); rs(s2);
int len1 = strlen(s1);
int len2 = strlen(s2);
rep(i, , len1)
s[n++] = s1[i];
s[n++] = '#';
rep(i, , len2)
s[n++] = s2[i];
s[n++] = ;
get_sa();
rep(i, , n)
{
if(height[i] < k)
{
sum = top = ;
continue;
}
int num = ;
while(top && height[i] < stac[top]) //维持单调递增栈 可能当前sa[i-1] < len1 但height是连续的 所以短板效应替换栈中元素
{ //而它自己如果sa[i-1] < len1 那么下面的 num是不加1的 即自己不算在内
sum -= (LL)(stac[top] - k + ) * cnt[top];
sum += (LL)(height[i] - k + ) * cnt[top];
num += cnt[top];
top--;
}
stac[++top] = height[i];
if(sa[i-] > len1) //扫描B串
{
sum += (LL)(height[i] - k + );
cnt[top] = num + ;
}
else
cnt[top] = num;
if(sa[i] < len1)
ans += sum;
}
rep(i, , n)
{
if(height[i] < k)
{
sum = top = ;
continue;
}
int num = ;
while(top && height[i] < stac[top])
{
sum -= (LL)(stac[top] - k + ) * cnt[top];
sum += (LL)(height[i] - k + ) * cnt[top];
num += cnt[top];
top--;
}
stac[++top] = height[i];
if(sa[i-] < len1) //扫描A串
{
sum += (LL)(height[i] - k + );
cnt[top] = num + ;
}
else
cnt[top] = num;
if(sa[i] > len1)
ans += sum;
}
printf("%lld\n", ans); } return ;
}
上一篇:JavaScript正则表达式验证大全(收集)


下一篇:Spring Boot使用过滤器和拦截器分别实现REST接口简易安全认证