C. Two strings 二分 + 预处理
http://codeforces.com/contest/762/problem/C
第一个串str[],第二个sub[]
预处理出prefix[i]表示sub的前i位和str[]的最长lcs去到str[]的最小的位置,如果某一位i不成立了,就设为inf
由于处理的时候,指针指向str[]的是单调递增的,所以预处理复杂度O(n),同样预处理suffix数组。
那么就是求一个位置,使得两个数组不会相交。
但是如果会相交,那么应该让那个数组退后一步呢,一开始用数组相邻差的绝对值来确定,因为移动得多的那个必定更优。
但是不然,test5就知道了。
于是乎就直接处理出所有的prefix[i],所对应的最优suffix[],这个可以在suffix[]中二分搞定。
话说二分可真有用。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
vector<int>pos[];
const int maxn = 1e5 + ;
char str[maxn], sub[maxn];
int lenstr, lensub;
int L, R;
int prefix[maxn];
int suffix[maxn];
void init() {
int p1 = ;
for (int i = ; i <= lensub; ++i) {
if (pos[sub[i] - 'a'].size() == ) break;
if (pos[sub[i] - 'a'].back() <= p1) break;
prefix[i] = upper_bound(pos[sub[i] - 'a'].begin(), pos[sub[i] - 'a'].end(), p1) - pos[sub[i] - 'a'].begin();
prefix[i] = pos[sub[i] - 'a'][prefix[i]];
p1 = prefix[i];
}
// for (int i = 1; i <= lensub; ++i) {
// cout << prefix[i] << " ";
// }
p1 = lenstr;
for (int i = lensub; i >= ; --i) {
while (p1 >= ) {
if (str[p1] == sub[i]) break;
p1--;
}
if (p1 == ) break;
suffix[i] = p1;
p1--;
}
// for (int i = 1; i <= lensub; ++i) {
// cout << suffix[i] << " ";
// }
}
void work() {
scanf("%s%s", str + , sub + );
lenstr = strlen(str + );
lensub = strlen(sub + );
memset(prefix, 0x3f, sizeof prefix);
memset(suffix, -, sizeof suffix);
for (int i = ; i <= lenstr; ++i) {
pos[str[i] - 'a'].push_back(i);
}
prefix[] = -;
suffix[lensub + ] = inf;
init();
int pos1 = inf, pos2 = inf;
for (int i = ; i <= lensub; ++i) {
if (prefix[i] == inf) {
pos1 = i - ;
break;
}
}
for (int i = lensub; i >= ; --i) {
if (suffix[i] == -) {
pos2 = i + ;
break;
}
}
if (prefix[] == inf && suffix[lensub] == -) {
cout << "-" << endl;
return;
}
if (pos1 == inf || pos2 == inf) {
cout << sub + << endl;
return;
}
if (pos1 == ) {
assert(pos2 != lensub + );
for (int i = pos2; i <= lensub; ++i) {
printf("%c", sub[i]);
}
return;
}
if (pos2 == lensub + ) {
for (int i = ; i <= pos1; ++i) {
printf("%c", sub[i]);
}
return;
}
int anslen = -;
for (int i = ; i <= pos1; ++i) {
int topos = upper_bound(suffix + pos2, suffix + + lensub, prefix[i]) - suffix;
// cout << suffix[topos] << endl;
if (anslen < i + lensub - topos + ) {
anslen = i + lensub - topos + ;
L = i + ;
R = topos - ;
}
}
// cout << L << " " << R << endl;
for (int i = ; i <= lensub; ++i) {
if (i == L) {
i = R;
continue;
}
printf("%c", sub[i]);
} } int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}
C. Two strings 二分 + 预处理的更多相关文章
- NOIP2015聪明的质检员[二分 | 预处理]
		背景 NOIP2011 day2 第二题 描述 小T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有 n 个矿石,从 1到n 逐一编号,每个矿石都有自己的重量 wi 以及价值vi .检验矿 ... 
- Codeforces Round #321 (Div. 2) B 二分+预处理
		B. Kefa and Company time limit per test 2 seconds memory limit per test 256 megabytes input standard ... 
- HDU 6274 二分+预处理(CCPC K题
		#include"bits/stdc++.h" #define db double #define ll long long #define vec vector<ll> ... 
- [luoguP3092] [USACO13NOV]没有找零No Change(状压DP + 二分)
		传送门 先通过二分预处理出来,每个硬币在每个商品处最多能往后买多少个商品 直接状压DP即可 f[i]就为,所有比状态i少一个硬币j的状态所能达到的最远距离,在加上硬币j在当前位置所能达到的距离,所有的 ... 
- EC R 86 D Multiple Testcases 构造 贪心 二分
		LINK:Multiple Testcases 得到很多种做法.其中O(n)的做法值得一提. 容易想到二分答案 check的时候发现不太清楚分配的策略. 需要先考虑如何分配 容易发现大的东西会对小的产 ... 
- 「Poetize3」Heaven Cow与God Bull
		描述 Description 给定一个整数n,求一个整数m,满足m<=n,并且m/phi(m)的值最大.注:phi(m)代表m的欧拉函数,即不大于m且与m互质的数的个数. 题解:m/phi(m) ... 
- D. 实验室传染病
		D. 实验室传染病 题意 给出 n 个人的位置,以及每个人的传染范围,当一个人患病时,他的传染范围内(包括边界上)的人全部会被感染并继续向外传播. 求以每个人为传染源最多有多少人被感染. 分析 首先二 ... 
- 【倍增】Tak and Hotels II @ABC044&ARC060/upcexam6463
		6463: Tak and Hotels II 时间限制: 1 Sec 内存限制: 128 MB 题目描述 N hotels are located on a straight line. The ... 
- CF #442 div2
		A 判断下5个名字出现了几次.pre数据巨弱,就这么一水题在std测刷掉了非常多的人.. /** @Date : 2017-10-24 16:04:41 * @FileName: A.cpp * @P ... 
随机推荐
- linux core文件设置
			http://blog.csdn.net/ctthuangcheng/article/details/8963551 linux core文件设置 分类: Linux OS Debugging Te ... 
- The Pilots Brothers' refrigerator-DFS路径打印
			I - The Pilots Brothers' refrigerator Time Limit:1000MS Memory Limit:65536KB 64bit IO Format ... 
- Java对象的创建过程
			//TODO https://www.cnblogs.com/chenyangyao/p/5296807.html 
- CSDN公开课:SCRUM敏捷开发(2015-8-19 免费)
			当前最火的敏捷可能就是SCRUM了.但敏捷无法落地.对人要求太高.老板对敏捷动机不良等问题怎样解决呢?我将在CSDN的公开课上为大家分享"SCRUM敏捷开发".各位朋友有杀错没放过 ... 
- [IT学习]华为全连接大会2017
			1.5分钟.3分钟.1分钟倒计时. 2.20万盏纽约街头的油灯接入电网,类比未来的公司IT系统会接入云? 3.1943年,全球只要5台计算机.不会的,但是会有5多云? 4.与航空业的联盟类比,云计算的 ... 
- linux路由表解析
			1 格式 Destination 这个和Genmask一起构成目标网络.路由是路由到目标网络,知道目标网络就可以到达目标路由器,然后在该网络中找到目标机器. Gateway 网关,数据包的下一跳.比如 ... 
- HDU3038 How Many Answers Are Wrong —— 带权并查集
			题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 200 ... 
- bat文件中调用传参的问题
			https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-lin ... 
- springmvc处理ajax跨域
			解决跨域问题:在web.xml中配置corsFilter mvc.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <!- ... 
- appium学习【三】:截图时,图片命令中包含当前的函数名,以区分错误是在哪个函数报的
			import sys funcName = sys._getframe().f_back.f_code.co_name #获取调用函数名 print sys._getframe().f_code.co ... 
