题意:

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

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

#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 ;
}

最新文章

  1. 使用ZwMapViewOfSection创建内存映射文件总结
  2. 利用Jquery获取、设置iframe中元素
  3. Centos7上启动vpn客户端失败问题处理
  4. 通过声明Attribute属性改变不同类的输出效果
  5. SOAP和WSDL的一些必要知识
  6. 安卓4.0下rem显示不正常的问题
  7. codevs3304水果姐逛水果街
  8. Java第四周学习日记
  9. 网页 HTML
  10. NET 平台下的插件化开发内核
  11. LYF电子书制作工具(CHM格式)
  12. 【Machine Learning in Action --4】朴素贝叶斯电子邮件垃圾过滤
  13. cmake的四个命令:add_compile_options、add_definitions、target_compile_definitions、build_command
  14. asp.net 分布式探讨之Session共享问题
  15. 20175315Mycp
  16. Android : Camera2/HAL3 框架分析
  17. 面试(一)-HashMap
  18. tf.nn.embedding_lookup
  19. Linux线程池的实现
  20. 关于docker构建镜像

热门文章

  1. Print Spooler 服务自动停止
  2. RabbitMQ入门:总结
  3. springboot+elasticsearch实现全局检索
  4. Java生成唯一ID
  5. SparkRDD编程实战
  6. NO.6:自学python之路------面向对象、内存持久化
  7. linux 其他知识目录
  8. [linux] tmux终端复用神器 [转载]
  9. Scrum立会报告+燃尽图(Final阶段第五次)
  10. Beta阶段基于spec评论作品