码迷,mamicode.com
首页 > 编程语言 > 详细

bnuoj 34990(后缀数组 或 hash+二分)

时间:2014-10-16 17:01:02      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   ar   java   

后缀数组倍增算法超时,听说用3DC可以勉强过,不愿写了,直接用hash+二分求出log(n)的时间查询两个字符串之间的任意两个位置的最长前缀.

我自己在想hash的时候一直在考虑hash成数值时MOD取多大,如果取10^18的话,那么两数相乘个就超LL了,但是取10^9的话又怕出现重复的可能大.后面才发现自己是sb,如果用unsigned long long 如果有溢出或者为负数是直接变成对(1<<64)取模了。 也就是无符号长整形运算自动帮你取模了。所以可以放心用hash

Justice String

Time Limit: 2000ms
Memory Limit: 65536KB
64-bit integer IO format: %lld      Java class name: Main
Type: 
None
 
 

Given two strings A and B, your task is to find a substring of A called justice string, which has the same length as B, and only has at most two characters different from B.

 

Input

The first line of the input contains a single integer T, which is the number of test cases.
For each test case, the first line is string A, and the second is string B.
Both string A and B contain lowercase English letters from a to z only. And the length of these two strings is between 1 and 100000, inclusive. 
 
 

Output

For each case, first output the case number as "Case #x: ", and x is the case number. Then output a number indicating the start position of substring C in A, position is counted from 0. If there is no such substring C, output -1.
And if there are multiple solutions, output the smallest one. 

 

 

Sample Input

3
aaabcd
abee
aaaaaa
aaaaa
aaaaaa
aabbb

Sample Output

Case #1: 2
Case #2: 0
Case #3: -1

Source

 
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
using namespace std;
#define N 100100

#define KEY 31

typedef unsigned long long ul;

char a[N],b[N];
ul base[N];
ul hha[N],hhb[N];
int lena,lenb;

ul gethash(int x,int y,ul g[])
{
    if(x>y)    return 0;
    return g[x]-g[y+1]*base[y+1-x];
}

int lcp(int pa,int pb)//求a串以pa为起始,与b串以pb为起始,最长的前缀
{
    int b=0,d=lenb-pb;//最小一个相同的都没有,最多有lenb个
    while(b<d)
    {
        int mid=(b+d+1)/2;
        if( gethash(pa,pa+mid-1,hha)==gethash(pb,pb+mid-1,hhb) )
            b=mid;
        else d=mid-1;
    }
    return b;
}

int main()
{
    int T;
    int tt=1;
    long long tmp=1;
    for(int i=0;i<N;i++)
    {
        base[i]=tmp;
        tmp*=KEY;
    }

    scanf("%d",&T);
    while(T--)
    {
        scanf("%s%s",a,b);
        lena=strlen(a);
        lenb=strlen(b);
        memset(hha,0,sizeof(hha));
        memset(hhb,0,sizeof(hhb));

        hha[lena]=0;
        for(int i=lena-1;i>=0;i--)
            hha[i] = hha[i+1]*KEY+a[i]-a;
        hhb[lenb]=0;
        for(int i=lenb-1;i>=0;i--)
            hhb[i] = hhb[i+1]*KEY+b[i]-a;

        int ans=-1;

        for(int i=0;i<=lena-lenb;i++)
        {
            int cnt=0;
            cnt += lcp(i+cnt,cnt);
            if(cnt>=lenb)
            {
                ans=i;
                break;
            }
            cnt++;
            if(cnt>=lenb)
            {
                ans=i;
                break;
            }
            cnt += lcp(i+cnt,cnt);
            if(cnt>=lenb)
            {
                ans=i;
                break;
            }
            cnt++;
            if(cnt>=lenb)
            {
                ans=i;
                break;
            }
            cnt += lcp(i+cnt,cnt);
            if(cnt>=lenb)
            {
                ans=i;
                break;
            }
        }
        printf("Case #%d: ",tt++);
        printf("%d\n",ans);
        //printf("%d %s\n",ans,a+ans);
    }
    return 0;
}

 

bnuoj 34990(后缀数组 或 hash+二分)

标签:des   style   blog   http   color   io   os   ar   java   

原文地址:http://www.cnblogs.com/chenhuan001/p/4028829.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!