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 二分 + 预处理的更多相关文章

  1. NOIP2015聪明的质检员[二分 | 预处理]

    背景 NOIP2011 day2 第二题 描述 小T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有 n 个矿石,从 1到n 逐一编号,每个矿石都有自己的重量 wi 以及价值vi .检验矿 ...

  2. Codeforces Round #321 (Div. 2) B 二分+预处理

    B. Kefa and Company time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. HDU 6274 二分+预处理(CCPC K题

    #include"bits/stdc++.h" #define db double #define ll long long #define vec vector<ll> ...

  4. [luoguP3092] [USACO13NOV]没有找零No Change(状压DP + 二分)

    传送门 先通过二分预处理出来,每个硬币在每个商品处最多能往后买多少个商品 直接状压DP即可 f[i]就为,所有比状态i少一个硬币j的状态所能达到的最远距离,在加上硬币j在当前位置所能达到的距离,所有的 ...

  5. EC R 86 D Multiple Testcases 构造 贪心 二分

    LINK:Multiple Testcases 得到很多种做法.其中O(n)的做法值得一提. 容易想到二分答案 check的时候发现不太清楚分配的策略. 需要先考虑如何分配 容易发现大的东西会对小的产 ...

  6. 「Poetize3」Heaven Cow与God Bull

    描述 Description 给定一个整数n,求一个整数m,满足m<=n,并且m/phi(m)的值最大.注:phi(m)代表m的欧拉函数,即不大于m且与m互质的数的个数. 题解:m/phi(m) ...

  7. D. 实验室传染病

    D. 实验室传染病 题意 给出 n 个人的位置,以及每个人的传染范围,当一个人患病时,他的传染范围内(包括边界上)的人全部会被感染并继续向外传播. 求以每个人为传染源最多有多少人被感染. 分析 首先二 ...

  8. 【倍增】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 ...

  9. CF #442 div2

    A 判断下5个名字出现了几次.pre数据巨弱,就这么一水题在std测刷掉了非常多的人.. /** @Date : 2017-10-24 16:04:41 * @FileName: A.cpp * @P ...

随机推荐

  1. 搭建gitserver

    1.下载gitosis代码出错 git clone git://eagain.net/gitosis.git Initialized empty Git repository in /tmp/gito ...

  2. iOS7获取UUID以及转换MD5

    近期项目开发,运用到要获取UUID转MD5,可是iOS7不能使用获取的UDID的接口(涉及到隐私),获取MAC地址的方式的接口在iOS7下也废弃了.眼下可能的就是获取UUID了,可是在iOS7下,UU ...

  3. SSH三大框架整合配置详细步骤(3)

    5 配置Spring2.5 5.1 基础配置 1)        导入spring包.下载spring-framework-2.5.6并解压后,在spring-framework-2.5.6" ...

  4. (22) java web的struts2框架的使用-struts配置文件

    1,配置文件的引用 struts中配置文件可以有多个,每个模块的包里面都可以单独设立一个struts配置文件. 主的配置文件,放在“src”文件夹下,可以引入其他配置文件,引入方式: <!-- ...

  5. HubbleDotNet引擎查询技术

    系统简介 HubbleDotNet 是一个基于.net framework 的开源免费的全文搜索数据库组件.开源协议是 Apache 2.0.HubbleDotNet提供了基于SQL的全文检索接口,使 ...

  6. [转]python的startswith()方法

    描述 Python startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False.如果参数 beg 和 end 指定值,则在指定范围内检查. 语法 ...

  7. Ubuntu 16.04安装Wireshark进行抓包

    技巧: 1.可以通过tcpdump抓取某个网卡的包,然后输出日志文件,通过Wireshark进行分析. 2.可以设置Wifi热点,然后通过手机连接这个热点,然后进行tcpdump的分析,然后输出日志文 ...

  8. Excel VBA 入门

    一.文件格式 要使用VBA,excel文件必须保存为启用宏的工作簿,即xlsm格式. 二.启动VBA编辑器 打开工作簿后,要启动VBA编辑器,有两种方法,一是在工作表的名字上面点击右键,选择“查看代码 ...

  9. SOLID总结(未完待续)

    SOLID原则是著名的面向对像设计五原则,之所以要引入这些原则,是为了给以防止代码腐化而进行的重构活动定下切实可行的目标. 随着系统开发的进行,产品代码不可避免地会不断腐化,就算在开发过程中很好地应用 ...

  10. poj 2987 Firing【最大权闭合子图+玄学计数 || BFS】

    玄学计数 LYY Orz 第一次见这种神奇的计数方式,乍一看非常不靠谱但是仔细想想还卡不掉 就是把在建图的时候把正权变成w*10000-1,负权变成w*10000+1,跑最大权闭合子图.后面的1作用是 ...