bnuoj 34990(后缀数组 或 hash+二分)
后缀数组倍增算法超时,听说用3DC可以勉强过,不愿写了,直接用hash+二分求出log(n)的时间查询两个字符串之间的任意两个位置的最长前缀.
我自己在想hash的时候一直在考虑hash成数值时MOD取多大,如果取10^18的话,那么两数相乘个就超LL了,但是取10^9的话又怕出现重复的可能大.后面才发现自己是sb,如果用unsigned long long 如果有溢出或者为负数是直接变成对(1<<64)取模了。 也就是无符号长整形运算自动帮你取模了。所以可以放心用hash
Justice String
64-bit integer IO format: %lld Java class name: Main
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
Output
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 ;
return g[x]-g[y+]*base[y+-x];
} int lcp(int pa,int pb)//求a串以pa为起始,与b串以pb为起始,最长的前缀
{
int b=,d=lenb-pb;//最小一个相同的都没有,最多有lenb个
while(b<d)
{
int mid=(b+d+)/;
if( gethash(pa,pa+mid-,hha)==gethash(pb,pb+mid-,hhb) )
b=mid;
else d=mid-;
}
return b;
} int main()
{
int T;
int tt=;
long long tmp=;
for(int i=;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,,sizeof(hha));
memset(hhb,,sizeof(hhb)); hha[lena]=;
for(int i=lena-;i>=;i--)
hha[i] = hha[i+]*KEY+a[i]-'a';
hhb[lenb]=;
for(int i=lenb-;i>=;i--)
hhb[i] = hhb[i+]*KEY+b[i]-'a'; int ans=-; for(int i=;i<=lena-lenb;i++)
{
int cnt=;
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 ;
}
bnuoj 34990(后缀数组 或 hash+二分)的更多相关文章
- 1402 后缀数组 (hash+二分)
描述 后缀数组 (SA) 是一种重要的数据结构,通常使用倍增或者DC3算法实现,这超出了我们的讨论范围.在本题中,我们希望使用快排.Hash与二分实现一个简单的 O(n log^2n ) 的后缀数组 ...
- 140. 后缀数组(hash + 二分 / 后缀数组)
题目链接 : https://www.acwing.com/problem/content/description/142/ Hash + 二分 #include <bits/stdc++.h& ...
- [poj 1743] Musical Theme 后缀数组 or hash
Musical Theme 题意 给出n个1-88组成的音符,让找出一个最长的连续子序列,满足以下条件: 长度大于5 不重叠的出现两次(这里的出现可以经过变调,即这个序列的每个数字全都加上一个整数x) ...
- CH1402 后缀数组【Hash】【字符串】【二分】
1402 后缀数组 0x10「基本数据结构」例题 描述 后缀数组 (SA) 是一种重要的数据结构,通常使用倍增或者DC3算法实现,这超出了我们的讨论范围.在本题中,我们希望使用快排.Hash与二分实现 ...
- CH 1402 - 后缀数组 - [字符串hash]
题目链接:传送门 描述 后缀数组 (SA) 是一种重要的数据结构,通常使用倍增或者DC3算法实现,这超出了我们的讨论范围. 在本题中,我们希望使用快排.Hash与二分实现一个简单的 $O(n \log ...
- 【BZOJ-4310】跳蚤 后缀数组 + ST表 + 二分
4310: 跳蚤 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 180 Solved: 83[Submit][Status][Discuss] De ...
- BZOJ3277 串 【后缀数组】【二分答案】【主席树】
题目分析: 用"$"连接后缀数组,然后做一个主席树求区间内不同的数的个数.二分一个前缀长度再在主席树上求不同的数的个数. 代码: #include<bits/stdc++.h ...
- BZOJ3473:字符串(后缀数组,主席树,二分,ST表)
Description 给定n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串? Input 第一行两个整数n,k. 接下来n行每行一个字符串. Output 一 ...
- 2019CCPC网络赛 C - K-th occurrence HDU - 6704(后缀数组+ST表+二分+主席树)
题意 求区间l,r的子串在原串中第k次出现的位置. 链接:https://vjudge.net/contest/322094#problem/C 思路 比赛的时候用后缀自动机写的,TLE到比赛结束. ...
随机推荐
- ThreadLocal源码
/* * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETA ...
- 利用docker创建支持centos的ssh镜像
创建docker镜像需要基础镜像,目前官方已提供了多种基础镜像,参见: https://hub.docker.com/explore/ 要想创建支持centos的ssh镜像,就需要以centos镜像为 ...
- NPOI(2.1.3)向excel中插入图片,xls文档图片插入成功,xlsx文档图片插入失败
众所周知,NPOI对xls和xlsx两个版本的excel文档的操作并没有一个统一的支持, 程序若想兼容这两个版本的操作,必须根据excel版本分别去调用HSSF和XSSF这两套操作库, 之前一直不明白 ...
- python selenium ---键盘事件
转自:http://www.cnblogs.com/fnng/p/3258946.html 本节重点: l 键盘按键用法 l 键盘组合键用法 l send_keys() 输入中文运行报错问题 键盘按键 ...
- ubuntu系统安装nginx出现的错误(依赖环境没有安装完)
报错信息: error: the HTTP image filter module requires the GD library. 编译参数:(或源安装) ./configure --prefix= ...
- Linux 系统 pptpd+radius+mysql 安装攻略
分类: 原文地址:Linux 系统 pptpd+radius+mysql 安装攻略 作者:wfeng .你所需要的软件 内核最好能升级到2.6 如果你是centos的用户,可以通过yum update ...
- Atitit.atiJsBridge 新特性v7q329
Atitit.atiJsBridge 新特性v7q329 atiJsBridge 未来计划 Postdata 图像上传的支持 Simp param计划 p1 p2 p3 p4 $method 的si ...
- html页面中js判断浏览器是否是IE浏览器及IE浏览器版本
HTML里: HTML代码中,在编写网页代码时,各种浏览器的兼容性是个必须考虑的问题,有些时候无法找到适合所有浏览器的写法,就只能写根据浏览器种类区别的代码,这时就要用到判断代码了.在HTML代码中, ...
- java 容器结构
先来看下层次关系. 如图所示:图中,实线边框的是实现类,折线边框的是抽象类,而点线边框的是接口 Collection(接口)├──List(接口)│ ├──LinkedList(实现类)│ ├ ...
- Android之2D图形(圆、直线、点)工具类 (持续更新)
public class Circle { private PointF centerPoint; private float radius; public PointF getCenterPoint ...