POJ2774(二分+哈希)
Long Long Message
Time Limit: 4000MS | Memory Limit: 131072K | |
Total Submissions: 27234 | Accepted: 11084 | |
Case Time Limit: 1000MS |
Description
The little cat lives in an unrich family, so he frequently comes to the mobile service center, to check how much money he has spent on SMS. Yesterday, the computer of service center was broken, and printed two very long messages. The brilliant little cat soon found out:
1. All characters in messages are lowercase Latin letters, without punctuations and spaces.
2. All SMS has been appended to each other – (i+1)-th SMS comes directly after the i-th one – that is why those two messages are quite long.
3. His own SMS has been appended together, but possibly a great many redundancy characters appear leftwards and rightwards due to the broken computer.
E.g: if his SMS is “motheriloveyou”, either long message printed by that machine, would possibly be one of “hahamotheriloveyou”, “motheriloveyoureally”, “motheriloveyouornot”, “bbbmotheriloveyouaaa”, etc.
4. For these broken issues, the little cat has printed his original text twice (so there appears two very long messages). Even though the original text remains the same in two printed messages, the redundancy characters on both sides would be possibly different.
You are given those two very long messages, and you have to output the length of the longest possible original text written by the little cat.
Background:
The SMS in Byterland mobile service are charging in dollars-per-byte. That is why the little cat is worrying about how long could the longest original text be.
Why ask you to write a program? There are four resions:
1. The little cat is so busy these days with physics lessons;
2. The little cat wants to keep what he said to his mother seceret;
3. POJ is such a great Online Judge;
4. The little cat wants to earn some money from POJ, and try to persuade his mother to see the doctor :(
Input
Output
Sample Input
yeshowmuchiloveyoumydearmotherreallyicannotbelieveit
yeaphowmuchiloveyoumydearmother
Sample Output
27
//2016.9.8
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ull unsigned long long using namespace std; const int N = ;
char s1[N], s2[N];
int len1, len2;
ull h1[N], h2[N], a[N], Pow[N]; ull get_hash(ull *h, int l, int r)
{
return h[r]-h[l]*Pow[r-l];
} bool solve(int len)
{
int cnt = ;
for(int i = len; i <= len1; i++)
a[cnt++] = get_hash(h1, i-len, i);
sort(a, a+cnt);
for(int i = len; i <= len2; i++)
{
ull h = get_hash(h2, i-len, i);
if(binary_search(a, a+cnt, h))
return true;
}
return false;
} int main()
{
scanf("%s%s", s1, s2);
len1 = strlen(s1);
len2 = strlen(s2);
int l = , r = max(len1, len2), mid;
Pow[] = ;
for(int i = ; i <= r; i++)
Pow[i] = Pow[i-]*;
h1[] = h2[] = ;
for(int i = ; i < len1; i++)
h1[i+] = h1[i]*+s1[i];
for(int i = ; i < len2; i++)
h2[i+] = h2[i]*+s2[i];
while(l < r)
{
mid = (l+r)>>;
if(solve(mid+))l = mid+;
else r = mid;
}
printf("%d\n", l); return ;
}
POJ2774(二分+哈希)的更多相关文章
- 2016vijos 1-1 兔子的字符串(后缀数组 + 二分 + 哈希)
题意: 给出一个字符串,至多将其划分为n部分,每一部分取出字典序最大的子串ci,最小化 最大的ci 先看一个简化版的问题: 给一个串s,再给一个s的子串t,问能否通过将串划分为k个部分,使t成为划分后 ...
- POJ 2774 后缀数组 || 二分+哈希
Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 35607 Accepted: 14 ...
- Codeforces 961F - k-substrings(二分+哈希)
Codeforces 题面传送门 & 洛谷题面传送门 介绍一种奇怪的 \(\Theta(n\log n)\) 的奇怪做法. 注意到这个"border 的长度必须是奇数"的条 ...
- [JSOI2008]Blue Mary的战役地图(二分+哈希)
Blue Mary最近迷上了玩Starcraft(星际争霸) 的RPG游戏.她正在设法寻找更多的战役地图以进一步提高自己的水平. 由于Blue Mary的技术已经达到了一定的高度,因此,对于用同一种打 ...
- poj 2002 Squares 几何二分 || 哈希
Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 15137 Accepted: 5749 Descript ...
- poj Squares n个点,共能组成多少个正方形 二分 + 哈希
题目链接:http://poj.org/problem?id=2002 测试数据: 41 00 11 10 090 01 02 00 21 22 20 11 12 14-2 53 70 05 20 有 ...
- UVA - 11996 Jewel Magic (Treap+二分哈希)
维护一个01序列,一共四种操作: 1.插入一个数 2.删除一个数 3.反转一个区间 4.查询两个后缀的LCP 用Splay或者Treap都可以做,维护哈希值,二分求LCP即可. 注意反转序列的时候序列 ...
- 【CodeForces】961 F. k-substrings 字符串哈希+二分
[题目]F. k-substrings [题意]给定长度为n的串S,对于S的每个k-子串$s_ks_{k+1}...s_{n-k+1},k\in[1,\left \lceil \frac{n}{2} ...
- Codeforces 109D String Transformation 字符串 哈希 KMP
原文链接https://www.cnblogs.com/zhouzhendong/p/CF109D.html 题目传送门 - CF109D 题意 给定两个字符串 $a,b$ ,求一组 $i,j$ 使得 ...
随机推荐
- Tesseract-OCR使用记录
Tesseract是一个开源的OCR(Optical Character Recognition,光学字符识别)引擎,可以识别多种格式的图像文件并将其转换成文本,目前已支持60多种语言(包括中文). ...
- php 语法中有 let 吗?
来源:http://stackoverflow.com/questions/9705281/with-and-let-in-php use(&$a) 用 use ($parameter) 这种 ...
- CentOS 5.8 x64 源码安装 samba-3.6.9
环境 CentOS 5.8 X64 wget http://www.samba.org/samba/ftp/stable/samba-3.6.9.tar.gz tar zxvf samb ...
- [iOS Animation]CALayer-图层时间
图层时间 时间和空间最大的区别在于,时间不能被复用 -- 弗斯特梅里克 在上面两章中,我们探讨了可以用CAAnimation和它的子类实现的多种图层动画.动画的发生是需要持续一段时间的,所以计时对整个 ...
- 《数据结构与算法分析:C语言描述》读书笔记------List的C语言实现
List的简单实现.在GCC下测试通过. list.h #ifndef _List_H /*List数据结构的简单实现*/ struct Node; typedef struct Node Node; ...
- Keil STM32调试,使用ST-Link下载程序时提示“flash timeout.reset the target and try it again”
参考: 很郁闷,买来没多久的 STM32F4-DISCOVERY 就挂了? STM32F103RB Jlink调试的时候出现flash timeout.reset the target and t ...
- iOS开发—— UIImagePickerController获取相册和拍照
一.简单的拍照显示,或是从相册中直接选取照片 #import "ViewController.h" @interface ViewController ()<UIImageP ...
- python内置函数all使用的坑
在代码的改造过程中,因为忽略了一个问题导致数据异常,在改造的过程中以及后续的review中都没注意到这个问题,单元测试也没有覆盖到,记录如下.这个坑在于all的使用上,如果参数为空元组或空列表时,返回 ...
- cocos2d-x 定时器selector的使用 :schedule的使用
在游戏设计时,我们需要不断的改变屏幕显示来反映游戏操作的效果,最简单的就是提示用户已经进行的游戏时间.为此,我们需要使用cocos2d-x内置的任务调度机制,即CCNode的schedule成员函数. ...
- oracle系列--解锁数据库
一.安装完成后解锁Scott数据库步骤: 进入SQL Plus 请输入用户名:sys输入口令:sys as sysdba //这里的口令是不可见的,注意空格SQL> alter user sc ...