Bnuoj 34990 (suffix array or hash+ two points)

Source: Internet
Author: User

Postfix array multiplication algorithm timed out, heard that with 3DC can reluctantly, do not want to write, directly with hash+ two minutes to find out log (n) time query two strings between the longest prefix of any two positions.

I think in the hash of the time has been considering the hash into the value of the MoD, if taken 10^18, then two number multiplied by the Super ll, but take 10^9 words and afraid of repetition may be large. Later found himself is SB, if with unsigned long long If there is an overflow or a negative number is changed directly to (1<<64) modulo. That is, the unsigned long shaping operation automatically helps you to take the mold. So you can safely use hash

Justice stringtime limit:2000msmemory limit:65536kb64-bit integer IO format:%lld Java class name: Main Prev Submit Status Statistics discuss Next

Given strings A and B, your task is to find a substring of a called Justice string, which have the same length as B, an D only had at the most of the characters different from B.

Inputthe first line of the input contains a single integer T, which is the number of the test cases. For each test case, the first line is string A, and the second is a string B.both string A and B contain lowercase 中文版 l Etters fromaToZOnly.   And the length of these strings is between 1 and 100000, inclusive. Outputfor each case, first output of 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 was counted from 0. If there is no such substring C, output-1.and If there is multiple solutions, output the smallest one.

Sample Input
3aaabcdabeeaaaaaaaaaaaaaaaaaaabbb
Sample Output
Case #1:2Case #2:0Case #3:-1
SOURCE2014 ACM-ICPC Beijing Invitational Programming Contest
#include <iostream>#include<stdio.h>#include<string.h>#include<string>#include<algorithm>#include<math.h>#include<stdlib.h>using namespacestd;#defineN 100100#defineKEY 31typedef unsignedLong Longul;CharA[n],b[n];ulBase[N];ul hha[n],hhb[n];intLena,lenb;ul Gethash (intXintY,ul g[]) {    if(x>y)return 0; returng[x]-g[y+1]*Base[y+1-x];}intLcpintPaintPb//ask a string to start with PA, with B string starting with PB, the longest prefix{    intb=0, D=LENB-PB;//least one of the same none, up to a LenB     while(b<d) {intMid= (b+d+1)/2; if(Gethash (pa,pa+mid-1, HHA) ==gethash (pb,pb+mid-1, HHB)) b=mid; Elsed=mid-1; }    returnb;}intMain () {intT; inttt=1; Long Longtmp=1;  for(intI=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(inti=lena-1; i>=0; i--) Hha[i]= hha[i+1]*key+a[i]-'a'; HHB[LENB]=0;  for(inti=lenb-1; i>=0; i--) Hhb[i]= hhb[i+1]*key+b[i]-'a'; intans=-1;  for(intI=0; i<=lena-lenb;i++)        {            intCnt=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 (suffix array or hash+ two points)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.