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. Material UI:很强大的CSS框架

    Material UI 是一款功能很强大,界面却十分清新简洁的CSS框架.Material UI利用了Google的Material Design 全新设计语言.而且让每个UI组件都变得很独立.因此开 ...

  2. SqlServer 经常使用分页方法总结

    SqlServer 经常使用分页方法总结 以下演示样例总结了,SqlServer数据库 经常使用分页方法,仅供学习參考 A. 使用 RowNumber 和 Between And 组合分页: /*** ...

  3. Unity5的关卡切换

    本文章由cartzhang编写,转载请注明出处. 全部权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/51055584 作者:car ...

  4. FFMpeg2.4.2 on Ubuntu14.04

     FFmpeg 2.4 "Fresnel" – is the leading multimedia framework, cross-platform solution tha ...

  5. 分享:Mac与Phy组成原理的简单分析

    原文链接:http://blog.chinaunix.net/uid-20528014-id-3050217.html 1.General 下图是网口结构简图.网口由CPU.MAC和PHY三部分组成. ...

  6. showModalDialog参数问题

    showModalDialog传递参数: 1.参数拼接放在url中,参数过长或带特殊字符时,容易出现问题. 2.参数放在showModalDialog属性里 <script type=" ...

  7. 【扩展知识2】函数strlen()和非函数sizeof的使用

    [扩展知识2]函数strlen()和非函数sizeof的使用 [扩展文件夹] strlen函数 sizeof ( 1 )函数strlen() 原型:size_tstrlen ( const char ...

  8. 问题:IIS部署 MVC项目 (autofac) 错误解决

    http://www.cnblogs.com/yelaiju/p/3375168.html Could not load file or assembly 'System.Core, Version= ...

  9. mysql 查询语句去重 通过sql去重

    url 在表中具有唯一性: 由于定时任务的多进程/代码的多线程/定时任务的调度/脚本的执行时间,导致,破坏了唯一性: SELECT COUNT(1) FROM test_error_tmpUNION ...

  10. cxf与struts2拦截器冲突的解决方案

    最近学习接口,学习了下cxf,用tomcat部署访问的时候,发现接口不能访问:百度了很多,最终找到比较好的解决方案: sturts2配置: <!-- 设置strus拦截器 --> < ...