每次询问求出两个字符串的最长公共前缀的长度 后缀数组+RMQ+lcp UVA 12338 - Anti-Rhyme Pairs
2015-10-03 21:52
666 查看
题目链接
题意:给定一些字符串,每次询问求出两个字符串的最长公共前缀的长度
思路:把字符串排序,就能求出height和rank数组,然后利用RMQ查询即可
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int N = 100005;
typedef pair<string, int> pii;
pii str
;
int save
;
int t, n, height
, rank
;
void init() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
cin >> str[i].first;
save[i] = str[i].first.length();
str[i].second = i;
}
sort(str, str + n);
for (int i = 0; i < n; i++) {
rank[str[i].second] = i;
if (i == 0) continue;
int len = min(str[i - 1].first.length(), str[i].first.length());
int j;
for (j = 0; j < len; j++) {
if (str[i - 1].first[j] != str[i].first[j])
break;
}
height[i] = j;
}
}
int best[N * 10][20];
void initRMQ() {
for (int i = 0; i < n; i++) best[i][0] = height[i];
for (int j = 1; (1<<j) <= n; j++)
for (int i = 0; i + (1<<j) - 1 < n; i++)
best[i][j] = min(best[i][j - 1], best[i + (1<<(j - 1))][j - 1]);
}
int lcp(int L, int R) {
L--; R--;
if (L == R) return save[L];
L = rank[L]; R = rank[R];
if (L > R) swap(L, R);
L++;
int k = 0;
while ((1<<(k + 1)) <= R - L + 1) k++;
return min(best[L][k], best[R - (1<<k) + 1][k]);
}
int main() {
int cas = 0;
scanf("%d", &t);
while (t--) {
init();
initRMQ();
int q, l, r;
scanf("%d", &q);
printf("Case %d:\n", ++cas);
while (q--) {
scanf("%d%d", &l, &r);
printf("%d\n", lcp(l, r));
}
}
return 0;
}
题意:给定一些字符串,每次询问求出两个字符串的最长公共前缀的长度
思路:把字符串排序,就能求出height和rank数组,然后利用RMQ查询即可
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int N = 100005;
typedef pair<string, int> pii;
pii str
;
int save
;
int t, n, height
, rank
;
void init() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
cin >> str[i].first;
save[i] = str[i].first.length();
str[i].second = i;
}
sort(str, str + n);
for (int i = 0; i < n; i++) {
rank[str[i].second] = i;
if (i == 0) continue;
int len = min(str[i - 1].first.length(), str[i].first.length());
int j;
for (j = 0; j < len; j++) {
if (str[i - 1].first[j] != str[i].first[j])
break;
}
height[i] = j;
}
}
int best[N * 10][20];
void initRMQ() {
for (int i = 0; i < n; i++) best[i][0] = height[i];
for (int j = 1; (1<<j) <= n; j++)
for (int i = 0; i + (1<<j) - 1 < n; i++)
best[i][j] = min(best[i][j - 1], best[i + (1<<(j - 1))][j - 1]);
}
int lcp(int L, int R) {
L--; R--;
if (L == R) return save[L];
L = rank[L]; R = rank[R];
if (L > R) swap(L, R);
L++;
int k = 0;
while ((1<<(k + 1)) <= R - L + 1) k++;
return min(best[L][k], best[R - (1<<k) + 1][k]);
}
int main() {
int cas = 0;
scanf("%d", &t);
while (t--) {
init();
initRMQ();
int q, l, r;
scanf("%d", &q);
printf("Case %d:\n", ++cas);
while (q--) {
scanf("%d%d", &l, &r);
printf("%d\n", lcp(l, r));
}
}
return 0;
}
相关文章推荐
- 深入理解 [NSBundle mainBundle]
- main cannot be resolved or is not a field
- SSD阵列卡方案优化:考虑使用RAID 50替代RAID 10
- 关于public static void main(string[] args)的典故
- hdu3487 Play with Chain
- daemon reparented / init --user http://blog.csdn.net/ixidof/article/details/42806239
- GOF 23 设计模式之 责任链模式(Chain Of Responsibility)
- HUST-2015 Multi-University Training Contest 9
- Linux内核中的进程组及会话 http://blog.csdn.net/daniel_h1986/article/details/6067685
- Gradle project sync failed. Please fix your project and try again
- HDU 1023 Train Problem II(组合数学)
- AI设计---Robot(v1.0),简单对话的实现
- 一. 作业管理 http://blog.csdn.net/lwbeyond/article/details/7361850
- Linux进程控制 http://blog.csdn.net/kennyrose/article/details/7533534
- linux进程间通信方式之匿名管道http://blog.csdn.net/guoyang1007/article/details/4720984
- Aizu 2450 Do use segment tree LCT
- AI---个人主页
- 1079. Total Sales of Supply Chain (25) - 类树操作
- 基于显卡的光栅化渲染器Gaius计划
- Codeforces Round #320 (Div. 2) 579A. Raising Bacteria