zoukankan      html  css  js  c++  java
  • UVA 12338

    UVA 12338 - Anti-Rhyme Pairs

    题目链接

    题意:给定一些字符串,每次询问求出两个字符串的最长公共前缀的长度

    思路:把字符串排序,就能求出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[N];
    int save[N];
    
    int t, n, height[N], rank[N];
    
    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:
    ", ++cas);
    	while (q--) {
    	    scanf("%d%d", &l, &r);
    	    printf("%d
    ", lcp(l, r));
    	}
        }
        return 0;
    }


  • 相关阅读:
    【Matlab】常用函数
    八大排序(稳定性讨论)
    【Matlab】用Matlab设计一个滤波器
    【转】安装Android的SDK中文教程(完整版,包括Eclipse安装)
    MyEclipse Tomcat配置+测试详解(不含Eclipse)
    浅谈Umd文件格式
    省公司二次面试~~
    Android中的Selector
    android之DPAD上下左右四个键控制
    ImageView.ScaleType
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/7375236.html
Copyright © 2011-2022 走看看