zoukankan      html  css  js  c++  java
  • UVA

    Description

    Download as PDF

    D

    Anti-Rhyme Pairs

    Input: Standard Input

    Output: Standard Output

    Often two words that rhyme also end in the same sequence of characters. We use this property to define the concept of an anti-rhyme. An anti-rhyme is a pair of words that have a similar beginning. The degree of anti-rhyme of a pair of words is further defined to be the length of the longest string S such that both strings start withS. Thus, “arboreal” and “arcturus” are an anti-rhyme pair of degree 2, while “chalkboard” and “overboard” are an anti-rhyme pair of degree 0.

     

    You are given a list of words. Your task is, given a list of queries in the form(i, j), print the degree of anti-rhyme for the pair of strings formed by thei-th and the j-th words from the list.

     

    Input

    Input consists of a number of test cases. The first line of input contains the number of test casesT (T ≤ 35). Immediately following this line are T cases.

     

    Each case starts with the number of strings N (1 ≤ N ≤ 105) on a line by itself. The followingN lines each contain a single non-empty string made up entirely of lower case English characters ('a' to 'z'), whose lengthL is guaranteed to be less than or equal to 10,000. In every case it is guaranteed thatN*L ≤ 106.

     

    The line following the last string contains a single integer Q (1 ≤ Q ≤ 106), the number of queries. Each of theQ lines following contain a query made up of two integers i and j separated by whitespace (1 ≤ i, j ≤ N).

     

    Output

    The output consists of T cases, each starting with a single line with“Case X:”, where X indicates the X-th case. There should be exactlyQ lines after that for each case. Each of those Q lines should contain an integer that is the answer to the corresponding query in the input.

     

    Sample Input                            Output for Sample Input

    2

    5

    daffodilpacm

    daffodiliupc

    distancevector

    distancefinder

    distinctsubsequence

    4

    1 2

    1 5

    3 4

    4 5

    2

    acm

    icpc

    2

    1 2

    2 2

    Case 1:

    8

    1

    8

    4

    Case 2:

    0

    4


    题意:给你n个字符串,让你求给定的两个串的最长公共前缀
    思路:预处理是无法达到时间要求的,那么我们能够先hash处理出每一个字符串每一个字符的哈希值(这个值是递增),然后就能二分的比較查询了

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    typedef long long ll;
    using namespace std;
    const int maxn = 1000005;
    const int seed = 31;
    
    char str[maxn];
    int start[maxn], len[maxn];
    ll hash[maxn];
    
    int cal(int a, int b) {
    	int l = 0, r = min(len[a], len[b]);	
    	while (l < r) {
    		int m = (l + r + 1) >> 1;
    		if (hash[start[a]+m-1] == hash[start[b]+m-1]) 
    			l = m;
    		else r = m-1;
    	}
    	return l;
    }
    
    int main() {
    	int t, n, cas = 1;
    	scanf("%d", &t);
    	while (t--) {
    		scanf("%d", &n);	
    		int cur = 0;
    		for (int i = 0; i < n; i++) {
    			scanf("%s", str+cur);
    			len[i] = strlen(str+cur);
    			start[i] = cur;
    			hash[cur] = str[cur] - 'a';
    			for (int j = 1; j < len[i]; j++)
    				hash[cur+j] = hash[cur+j-1] * seed + str[cur+j] - 'a';
    			cur += len[i];
    		}
    		scanf("%d", &n);
    		printf("Case %d:
    ", cas++);
    		int a, b;
    		while (n--) {
    			scanf("%d%d", &a, &b);	
    			printf("%d
    ", cal(a-1, b-1));
    		}
    	}
    	return 0;
    }


  • 相关阅读:
    ffmpeg处理RTMP流媒体的命令大全
    人像摄影技巧——镜头差异可改变脸部印象
    windows操作系统自带的TCP端口转发
    IIS7.5如何限制某UserAgent 禁止访问
    JS显示上一周
    mysql主从复制(超简单)
    0001-BUGIFX-Magento-Zend-Framework-1-PHP5.6.patch
    Nginx启用Gzip压缩js无效的原因
    开启Nginx的gzip压缩功能详解
    Python 中的垃圾回收机制
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4010491.html
Copyright © 2011-2022 走看看