您的位置:首页 > 其它

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

2014-10-16 15:02 381 查看
后缀数组倍增算法超时,听说用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
Prev
Submit Status Statistics Discuss
Next
Type:
None

None

Graph Theory

2-SAT

Articulation/Bridge/Biconnected Component

Cycles/Topological Sorting/Strongly Connected Component

Shortest Path

Bellman Ford

Dijkstra/Floyd Warshall

Euler Trail/Circuit

Heavy-Light Decomposition

Minimum Spanning Tree

Stable Marriage Problem

Trees

Directed Minimum Spanning Tree

Flow/Matching

Graph Matching

Bipartite Matching

Hopcroft–Karp Bipartite Matching

Weighted Bipartite Matching/Hungarian Algorithm

Flow

Max Flow/Min Cut

Min Cost Max Flow

DFS-like

Backtracking with Pruning/Branch and Bound

Basic Recursion

IDA* Search

Parsing/Grammar

Breadth First Search/Depth First Search

Advanced Search Techniques

Binary Search/Bisection

Ternary Search

Geometry

Basic Geometry

Computational Geometry

Convex Hull

Pick's Theorem

Game Theory

Green Hackenbush/Colon Principle/Fusion Principle

Nim

Sprague-Grundy Number

Matrix

Gaussian Elimination

Matrix Exponentiation

Data Structures

Basic Data Structures

Binary Indexed Tree

Binary Search Tree

Hashing

Orthogonal Range Search

Range Minimum Query/Lowest Common Ancestor

Segment Tree/Interval Tree

Trie Tree

Sorting

Disjoint Set

String

Aho Corasick

Knuth-Morris-Pratt

Suffix Array/Suffix Tree

Math

Basic Math

Big Integer Arithmetic

Number Theory

Chinese Remainder Theorem

Extended Euclid

Inclusion/Exclusion

Modular Arithmetic

Combinatorics

Group Theory/Burnside's lemma

Counting

Probability/Expected Value

Others

Tricky

Hardest

Unusual

Brute Force

Implementation

Constructive Algorithms

Two Pointer

Bitmask

Beginner

Discrete Logarithm/Shank's Baby-step Giant-step Algorithm

Greedy

Divide and Conquer

Dynamic Programming
Tag it!

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

2014 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 namespace std;
#define N 100100

#define KEY 31

typedef unsigned long long ul;

char a
,b
;
ul base
;
ul hha
,hhb
;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: