标签: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
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.
3 aaabcd abee aaaaaa aaaaa aaaaaa aabbb
Case #1: 2 Case #2: 0 Case #3: -1
#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; }
标签:des style blog http color io os ar java
原文地址:http://www.cnblogs.com/chenhuan001/p/4028829.html