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 ...
随机推荐
- 读书笔记-HBase in Action-第三部分应用-(2)GIS系统
本章介绍用HBase存储.高效查询地理位置信息. Geohash空间索引 考虑LBS应用中常见的两个问题:1)查找离某地近期的k个地点.2)查找某区域内地点. 假设要用HBase实现高效查找,首先要考 ...
- vue 定义全局函数
方法一:main.js 注入 (1)在main.js中写入函数 Vue.prototype.changeData = function (){ alert('执行成功'); } (2)在所有组件里可调 ...
- hdoj 2046 骨牌铺方格 【DP】+【斐波那契】
dp果然不是好学的... 第n个,即2*n时,可由第n-1个的竖直排列再加一个,和第n-2个中横着排两个 所以f(n) = 1×f(n-1) + 1×f(n-2): 骨牌铺方格 Time Limit: ...
- java 配置时遇到的问题及解决办法
1. 最近JDK更新很频繁,以至于我安装时版本太多,选择也会出现问题 首先,确定你选择的是32位版本还是64位版本(貌似64位系统下也可以安装32位的JDK), 这个相当重要,因为这个会影响到ecli ...
- Repeater控件前台复杂逻辑判断
虽然现在开发大都是前后台ajax的方式,但是还有部分项目用后台cs代码+服务器控件开发的方式,小弟今天就遇到了一个 repeater显示列表,有一个字段是state状态,数据库里面存的是0 1 2类似 ...
- 2016/05/11 Thinkphp 3.2.2 验证码 使用 及校验
先新建一个公共控制器,用于放置验证码的实例化代码(不用新建控制器也行,任意公共控制器都可以). 例如:PublicController.class.php 4 5 6 7 8 9 10 11 12 1 ...
- 关于wireshark
1 对https进行抓包,或者说抓包经过了ssl加密的包 只要有rsa private key就可以了. https://wiki.wireshark.org/SSL 2 对浏览器访问的https的网 ...
- bzoj4594: [Shoi2015]零件组装机
论静态查错的重要性...乱搞题真难调 首先这题看起来就是要分治检验了. 考虑对于区间[l,r],分成[l,p-1]和[p,r]使得这两个区间合并可以得到[l,r],并且要保证后面一个区间较大 设前一个 ...
- fcitx-configtool
配置输入法
- 并不对劲的bzoj4199: [Noi2015]品酒大会
传送门-> 又称普及大会. 这题没什么好说的……后缀自动机裸题……并不对劲的人太菜了,之前照着标程逐行比对才过了这道题,前几天刚刚把这题一遍写对…… 这题的输出和某两点相同后缀的长度有关,那么把 ...