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

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

```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.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;
}
}
};
}

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();
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);
}
}
}```

98 篇文章33 人订阅

0 条评论

## 相关文章

991

20710

2895

### 面试题53（考察求职者对String声明变量在jvm中的存储方法）

(单选题) 1、有如下一段代码，请选择其运行结果（） public class StringDemo{ private static final Stri...

2873

3366

### Java Collection开发技巧

Java Collection(集合) ? 集合中的一些技巧： 通过Collections类的静态方法，可以对集合进行一些操作 1 java....

21510

2048

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

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

2867

981

1453