后缀数组可解。使用单调栈优化。

 /* 3415 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxl = 1e5+;
const int maxn = maxl * ;
char as[maxl], bs[maxl];
int a[maxn];
int rank[maxn], height[maxn], sa[maxn];
int wa[maxn], wb[maxn], wc[maxn], wv[maxn];
int S[maxn][];
int K; bool cmp(int *r, int a, int b, int l) {
return r[a]==r[b] && r[a+l]==r[b+l];
} void da(int *r, int *sa, int n, int m) {
int i, j, *x=wa, *y=wb, *t, p; for (i=; i<m; ++i) wc[i] = ;
for (i=; i<n; ++i) wc[x[i]=r[i]]++;
for (i=; i<m; ++i) wc[i] += wc[i-];
for (i=n-; i>=; --i) sa[--wc[x[i]]] = i;
for (j=,p=; p<n; j*=, m=p) {
for (p=,i=n-j; i<n; ++i) y[p++] = i;
for (i=; i<n; ++i) if (sa[i] >= j) y[p++] = sa[i] - j;
for (i=; i<n; ++i) wv[i] = x[y[i]];
for (i=; i<m; ++i) wc[i] = ;
for (i=; i<n; ++i) wc[wv[i]]++;
for (i=; i<m; ++i) wc[i] += wc[i-];
for (i=n-; i>=; --i) sa[--wc[wv[i]]] = y[i];
for (t=x,x=y,y=t, x[sa[]]=, p=, i=; i<n; ++i)
x[sa[i]] = cmp(y, sa[i-], sa[i], j) ? p-:p++;
}
} void calheight(int *r, int *sa, int n) {
int i, j, k = ; for (i=; i<=n; ++i) rank[sa[i]] = i;
for (i=; i<n; height[rank[i++]]=k)
for (k?k--:, j=sa[rank[i]-]; r[i+k]==r[j+k]; ++k);
} void printSa(int n) {
for (int i=; i<=n; ++i)
printf("%d ", sa[i]);
putchar('\n');
} void printHeight(int n) {
for (int i=; i<=n; ++i)
printf("%d ", height[i]);
putchar('\n');
} void solve() {
int n = , nn; for (int i=; ; ++i) {
if (as[i] == '\0') {
nn = i;
break;
}
a[n++] = as[i];
}
a[n++] = ;
for (int i=; ; ++i) {
if (bs[i] == '\0') {
break;
}
a[n++] = bs[i];
}
a[n] = ; da(a, sa, n+, );
calheight(a, sa, n); int top;
__int64 tot, ans = ; // handle as with bs
top = ;
tot = ;
rep(i, , n+) {
if (height[i] < K) {
top = ;
tot = ;
} else {
int cnt = ;
if (sa[i-] < nn) {
++cnt;
tot += height[i] - K + ;
}
while (top && height[i]<=S[top-][]) {
--top;
tot -= 1LL * S[top][] * (S[top][] - height[i]);
cnt += S[top][];
}
S[top][] = height[i];
S[top][] = cnt;
++top;
if (sa[i] > nn)
ans += tot;
}
} tot = ;
top = ;
rep(i, , n+) {
if (height[i] < K) {
tot = ;
top = ;
} else {
int cnt = ;
if (sa[i-] > nn) {
++cnt;
tot += height[i] - K + ;
}
while (top && height[i]<=S[top-][]) {
--top;
tot -= 1LL * S[top][] * (S[top][] - height[i]);
cnt += S[top][];
}
S[top][] = height[i];
S[top][] = cnt;
++top;
if (sa[i] < nn)
ans += tot;
}
} printf("%I64d\n", ans);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif while (scanf("%d", &K)!=EOF && K) {
scanf("%s%s", as, bs);
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

数据发生器。

 from random import randint, shuffle
import shutil
import string def GenDataIn():
with open("data.in", "w") as fout:
t = 20
bound = 10**2
lc = list(string.lowercase)
for tt in xrange(t):
k = randint(1, 10)
fout.write("%d\n" % (k))
length = randint(100, 500)
line = ""
for i in xrange(length):
idx = randint(0, 25)
line += lc[idx]
fout.write("%s\n" % line)
length = randint(100, 500)
line = ""
for i in xrange(length):
idx = randint(0, 25)
line += lc[idx]
fout.write("%s\n" % line)
fout.write("0\n") def MovDataIn():
desFileName = "F:\eclipse_prj\workspace\hdoj\data.in"
shutil.copyfile("data.in", desFileName) if __name__ == "__main__":
GenDataIn()
MovDataIn()

【POJ】3415 Common Substrings的更多相关文章

  1. poj 3415 Common Substrings(后缀数组+单调栈)

    http://poj.org/problem?id=3415 Common Substrings Time Limit: 5000MS   Memory Limit: 65536K Total Sub ...

  2. POJ 3415 Common Substrings(后缀数组 + 单调栈)题解

    题意: 给两个串\(A.B\),问你长度\(>=k\)的有几对公共子串 思路: 先想一个朴素算法: 把\(B\)接在\(A\)后面,然后去跑后缀数组,得到\(height\)数组,那么直接\(r ...

  3. 【POJ】1704 Georgia and Bob(Staircase Nim)

    Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, ...

  4. 【POJ】1067 取石子游戏(博弈论)

    Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...

  5. 【SPOJ】Longest Common Substring II (后缀自动机)

    [SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...

  6. 【SPOJ】Longest Common Substring(后缀自动机)

    [SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...

  7. 【题解】Greatest Common Increasing Subsequence

    [题解]Greatest Common Increasing Subsequence vj 唉,把自己当做DP入门选手来总结这道题吧,我DP实在太差了 首先是设置状态的技巧,设置状态主要就是要补充不漏 ...

  8. 【SPOJ】Longest Common Substring II

    [SPOJ]Longest Common Substring II 多个字符串求最长公共子串 还是将一个子串建SAM,其他字符串全部跑一边,记录每个点的最大贡献 由于是所有串,要对每个点每个字符串跑完 ...

  9. 【SPOJ】Longest Common Substring

    [SPOJ]Longest Common Substring 求两个字符串的最长公共子串 对一个串建好后缀自动机然后暴力跑一下 废话 讲一下怎么跑吧 从第一个字符开始遍历,遍历不到了再沿着\(pare ...

随机推荐

  1. firefox 自定义快捷键

    firefox 更新到44或45,发现原来的更改快捷键的扩展没了!!!

  2. php入门变量之数字

    在介绍变量时,我明确指出PHP具有整型和浮点型(小数)数字类型.但是,依据我的经验,这两种类型都可以归类到一般的数字之下(在极大程度上是这样的). 下面列举下PHP中有效的数字类型的变量: 8 3.1 ...

  3. Demo学习: Cookies Demo

    Cookies Demo 浏览器Cookies的读写,最常用的就是记录用户的登录信息,在项目里做登录界面时也用到了Cookies功能. procedure TMainForm.UniButton2Cl ...

  4. linux乱码问题:LANG变量的秘诀

    对于国内的Linux用户,经常烦恼的一个问题是:系统常常在需要显示中文的时候却显示成了乱码,而由于某些原因,需要英文界面的系统的时候,却苦于系统不能正常输入和显示中文.另外,由于大部分主要Linux发 ...

  5. python 实现文件批量拷贝

    场景:某个文件夹下面包含数量巨大的文件,需求需要将这些文件按组(比如5000个一组)存放到不同的目录中去. # Filename: CopyFiles.py import os import os.p ...

  6. windows 与fedora时间差

    windows 默认BIOS时间当前时间UTC+时区, 按北京时间时区,就是要加8个小时. Linux默认BIOS时间是UTC时间,所以同一机子上装WINDOWS与LINUX时间上会差8个小时.这问题 ...

  7. 【学习总结】【多线程】 多线程概要 & GDC & NSOperation

    基本需要知道的 :  进程 :  简单点来说就是,操作系统中正在运行的一个应用程序,每个进程之间是独立的,每个进程均运行在受保护的内存空间内 线程 :  一个进程(进程)想执行任务,必须有线程(所以, ...

  8. 基于Python+协程+多进程的通用弱密码扫描器

    听说不想扯淡的程序猿,不是一只好猿.所以今天来扯扯淡,不贴代码,只讲设计思想. 0x00 起 - 初始设计 我们的目标是设计一枚通用的弱密码扫描器,基本功能是针对不同类型的弱密码,可方便的扩展,比如添 ...

  9. Many To one 多对一

    一.创建实体类:多方存一方的对象.set/get 二.编写对象的xml文件 别忘记在confg.xml映射! 三.编写接口 四.方法测试

  10. ng-blur失去焦点执行事件

    <label class="item item-input item-stacked-label"> <span class="input-label& ...