传送门:POJ 3415: Common Substrings
题意:
公共子串:统计A和B长度不小于K的公共子串个数。
思路:
一开始想当然的求LCP数组,把LCP遍历一遍,大于K的,ans不断累加ans += lcp[i] - k + 1
,但这只是一部分解,因为所求的只是相邻后缀的最长公共前缀。
一种直观的做法时,两个for循环,对于当前位置属于S的LCP时,向下找属于T的后缀,把每个后缀的最长对应公共前缀加一遍,但TLE了,此处需要利用单调性来简化时间复杂度。采用记忆化手段,用栈。
自己写了一种,但各别样例过不了,并不清楚原因。此处代码参考hankcs的代码,具体思路如下:对应于S的lcp,先不管三七二十一,贡献值都以最大的加,但我们知道,中间如果lcp变小,意味着后缀的公共前缀在不断减小,此时需要按照最小的算,只需要在贡献值上,删除原先多加的即可。针对T的lcp,同样处理。
代码如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
public class Main{
String INPUT = "./data/judge/201709/P3415.txt";
public static void main(String[] args) throws IOException {
new Main().run();
}
class SuffixArray{
int k = 1;
int n;
Integer[] sa;
int[] rank, tmp, lcp;
SuffixArray(char[] cs){
n = cs.length;
sa = new Integer[n + 1];
rank = new int[n + 1];
tmp = new int[n + 1];
lcp = new int[n];
for (int i = 0; i <= n; ++i) {
sa[i] = i;
rank[i] = i < n ? cs[i] : -1;
}
for (k = 1; k <= n; k <<= 1) {
Arrays.sort(sa, cmp);
tmp[sa[0]] = 0;
for (int i = 1; i <= n; ++i) {
tmp[sa[i]] = tmp[sa[i - 1]] + (cmp.compare(sa[i - 1], sa[i]) < 0 ? 1 : 0);
}
for (int i = 0; i <= n; ++i) {
rank[i] = tmp[i];
}
}
// lcp
int h = 0;
for (int i = 0; i <= n; ++i) rank[sa[i]] = i;
for (int i = 0; i < n; ++i) {
int j = sa[rank[i] - 1];
if (h > 0) h --;
for (;j + h < n && i + h < n; ++h) {
if (cs[j + h] != cs[i + h]) break;
}
lcp[rank[i] - 1] = h;
}
}
Comparator<Integer> cmp = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
int i = o1;
int j = o2;
if (rank[i] != rank[j]) return rank[i] - rank[j];
else {
int ri = i + k <= n ? rank[i + k] : -1;
int rj = j + k <= n ? rank[j + k] : -1;
return ri - rj;
}
}
};
}
void read() {
while (true) {
int k = ni();
if (k == 0) break;
String s = ns();
String t = ns();
SuffixArray sa = new SuffixArray((s + '$' + t).toCharArray());
long ans = 0;
ans += solve(true, s.length(), k, sa);
ans += solve(false, s.length(), k, sa);
out.println(ans);
}
}
int[][] stack;
int top;
long sum;
long solve(boolean isS, int sLen, int k, SuffixArray sa) {
long ans = 0;
sum = 0;
top = 0;
stack = new int[sa.n + 16][2];
for (int i = 0; i < sa.n; ++i) {
if (sa.lcp[i] < k) {
top = 0;
sum = 0;
}
else {
int size = 0;
if ((isS && sa.sa[i] < sLen) || (!isS && sa.sa[i] > sLen)) {
size ++;
sum += sa.lcp[i] - k + 1;
}
while (top > 0 && sa.lcp[i] < stack[top - 1][0]) {
--top;
sum -= stack[top][1] * (stack[top][0] - sa.lcp[i]);
size += stack[top][1];
}
if (size > 0) {
stack[top][0] = sa.lcp[i];
stack[top][1] = size;
top ++;
}
if ((isS && sa.sa[i + 1] > sLen) || (!isS && sa.sa[i + 1] < sLen)) {
ans += sum;
}
}
}
return ans;
}
FastScanner in;
PrintWriter out;
void run() throws IOException {
boolean oj;
try {
oj = ! System.getProperty("user.dir").equals("F:\\java_workspace\\leetcode");
} catch (Exception e) {
oj = System.getProperty("ONLINE_JUDGE") != null;
}
InputStream is = oj ? System.in : new FileInputStream(new File(INPUT));
in = new FastScanner(is);
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
read();
out.flush();
if (!oj){
System.out.println("[" + (System.currentTimeMillis() - s) + "ms]");
}
}
public boolean more(){
return in.hasNext();
}
public int ni(){
return in.nextInt();
}
public long nl(){
return in.nextLong();
}
public double nd(){
return in.nextDouble();
}
public String ns(){
return in.nextString();
}
public char nc(){
return in.nextChar();
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
boolean hasNext;
public FastScanner(InputStream is) throws IOException {
br = new BufferedReader(new InputStreamReader(is));
hasNext = true;
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
hasNext = false;
return "##";
}
}
return st.nextToken();
}
String next = null;
public boolean hasNext(){
next = nextToken();
return hasNext;
}
public int nextInt() {
if (next == null){
hasNext();
}
String more = next;
next = null;
return Integer.parseInt(more);
}
public long nextLong() {
if (next == null){
hasNext();
}
String more = next;
next = null;
return Long.parseLong(more);
}
public double nextDouble() {
if (next == null){
hasNext();
}
String more = next;
next = null;
return Double.parseDouble(more);
}
public String nextString(){
if (next == null){
hasNext();
}
String more = next;
next = null;
return more;
}
public char nextChar(){
if (next == null){
hasNext();
}
String more = next;
next = null;
return more.charAt(0);
}
}
}