# 挑战程序竞赛系列（73）：4.7高度数组（3）

## 挑战程序竞赛系列（73）：4.7高度数组（3）

```import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Map;
import java.util.StringTokenizer;

public class Main{

String INPUT = "./data/judge/201709/P3729.txt";

public static void main(String[] args) throws IOException {
new Main().run();
}

class SuffixArray {
int k = 1;
int n;
Integer[] sa;
int[] rank, tmp;
int[] lcp;

SuffixArray(int[] arra) {
n = arra.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 ? arra[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 (; i + h < n && j + h < n; ++h) {
if (arra[i + h] != arra[j + 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;
}
}
};

}

while (more()) {
int m = ni();
int n = ni();
int k = ni();
int[] arra = new int[m + n + 1];
for (int i = 0; i < m; ++i) {
arra[i] = ni();
arra[i] ++;
}
arra[m] = 10000 + 8;
for (int i = 0; i < n; ++i) {
arra[i + m + 1] = ni();
arra[i + m + 1] ++;
}

SuffixArray sa = new SuffixArray(arra);
out.println(solve(k, m, sa) - solve(k + 1, m, sa));
}
}

int solve(int k, int m, SuffixArray sa) {
int ans = 0, A = 0, B = 0;
for (int i = 0; i < sa.n; i++) {// 统计lcp[i]>=k的连续区间内的A子串
if (sa.lcp[i] < k) {
if (B > 0)
ans += A;
A = 0;
B = 0;
}
if (sa.sa[i + 1] < m)
A++;
else
B++;
}
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();
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 {
StringTokenizer st;
boolean hasNext;

public FastScanner(InputStream is) throws IOException {
hasNext = true;
}

public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
} 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);
}
}

static class ArrayUtils {

public static void fill(int[][] f, int value) {
for (int i = 0; i < f.length; ++i) {
Arrays.fill(f[i], value);
}
}

public static void fill(int[][][] f, int value) {
for (int i = 0; i < f.length; ++i) {
fill(f[i], value);
}
}

public static void fill(int[][][][] f, int value) {
for (int i = 0; i < f.length; ++i) {
fill(f[i], value);
}
}
}
}```

98 篇文章33 人订阅

0 条评论

## 相关文章

2223

### salesforce 零基础开发入门学习（二）变量基础知识，集合，表达式，流程控制语句

salesforce如果简单的说可以大概分成两个部分：Apex,VisualForce Page. 其中Apex语言和java很多的语法类似，今天总结的是一些简...

2847

1073

2826

### 详述 Java 语言中的 String、StringBuffer 和 StringBuilder 的使用方法及区别

1 简介 在 Java 语言中，共有 8 个基本的数据类型，分别为：byte、short、int、long、float、double、boolean和char，...

2045

692

### Java正则表达式入门

1.定义：正则表达式是一种可以用于模式匹配和替换的规范，一个正则表达式就是由普通的字符（例如字符a到z）以及特殊字符（元字符）组成的文字模式，它 用以描述在查...

772

2895

### 深入理解String类

String是java中的字符串。String类是不可变的,对String类的任何改变,都是返回一个新的String类对象。String不属于8种基本数据类型，...

512

2635