Codeforces Round #590 (Div. 3) F
题意:
给出一个只含前\(20\)个字符的字符串,现在可以选择一段区间进行翻转,问区间中字符各不相同时,最长长度为多少。
思路:
- 首先,容易将题意转换为选择两个字符各不相同的区间,然后长度相加取最大;
- 注意到字符串中满足条件的区间长度不超过\(20*n\),那么处理出所有区间,现在任务即为找到两个区间,其字符各不想同,且长度和最大;
- 因为最多\(20\)个字符,将满足条件的区间转换为二进制数,任务转换为找到两个数\(a_i,a_j\)满足\(a_i\&a_j=0\)且二进制为\(1\)的个数和最大;
- 构造\(b\)数组,且\(b_i\)为\(a_i\)按位取反后的值,之后问题转换为子集问题,对于每一个\(state\),我们找到子集中\(a_i\)二进制\(1\)个数的最大值,之后用\(a_i,b_i\)更新答案即可。
- 直接枚举子集复杂度为\(3^n\),可能会\(T\),那么\(dp\)优化一下就行(即高维前缀和,似乎也叫\(sos(sum\ of\ subset)\ dp\)。
其实只要发现满足条件的区间不超过\(20*n\)个,之后的思路就比较自然了,最后的\(dp\)优化也要有知识储备才出得来。
挺不错的一个题。
代码如下:
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
//#define Local
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1666666;
char s[N];
int n;
int a[N], b[N];
void run() {
memset(b, -1, sizeof(b));
cin >> s + 1;
n = strlen(s + 1);
int lim = (1 << 20) - 1;
for(int i = 1; i <= n; i++) {
int x = 0, c = 0;
for(int j = i; j <= n; j++) {
int bit = s[j] - 'a';
if(x >> bit & 1) break;
x |= (1 << bit); ++c;
a[x] = c; b[lim ^ x] = c;
}
}
for(int j = 0; j < 20; j++) {
for(int i = 0; i < lim; i++) {
if(i >> j & 1) a[i] = max(a[i ^ (1 << j)], a[i]);
}
}
int ans = 0;
for(int i = 0; i < lim; i++) {
if(b[i] >= 0) {
ans = max(ans, a[i] + b[i]);
}
}
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
#ifdef Local
freopen("../input.in", "r", stdin);
freopen("../output.out", "w", stdout);
#endif
run();
return 0;
}
Codeforces Round #590 (Div. 3) F的更多相关文章
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #501 (Div. 3) F. Bracket Substring
题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces Round #590 (Div. 3) Editorial
Codeforces Round #590 (Div. 3) Editorial 题目链接 官方题解 不要因为走得太远,就忘记为什么出发! Problem A 题目大意:商店有n件商品,每件商品有不同 ...
- Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)
题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...
- Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)
题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
- Codeforces Round #325 (Div. 2) F. Lizard Era: Beginning meet in the mid
F. Lizard Era: Beginning Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
- Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)
题目地址:http://codeforces.com/contest/474/problem/F 由题意可知,最后能够留下来的一定是区间最小gcd. 那就转化成了该区间内与区间最小gcd数相等的个数. ...
随机推荐
- LVS基本知识
前言 linux集群类型 LB -->负载均衡集群(Load Balancing) HA-->高可用集群(High Availiablity) HP-->高性性集群(High ...
- C++ try catch 示例代码
#include<iostream> void f1() { throw std::string("error happen"); } void f2() { try ...
- css3/sass 样式记录
css3 width: calc(50% - 10px) sass 1.奇偶行 .classNameA { background:red; &:nth-child(even) { backgr ...
- verilog 常见单元描述
半加器: //行为级建模 module half_adder2(a, b, sum, c_out); input a, b; output sum, c_out; assign {c_out, sum ...
- 42 在Raspberry Pi上安装dlib表情识别
https://www.jianshu.com/p/848014d8dea9 https://www.pyimagesearch.com/2017/05/01/install-dlib-raspber ...
- [日常] NOI前划水日记
NOI前划水日记 开坑记录一下每天的效率有多低 5.24 早上被春哥安排了一场NEERC(不过怎么是qualification round啊) 省队势力都跑去参加THU/PKU夏令营了...剩下四个D ...
- c# lock 锁
lock语句 lock 语句获取给定对象的互斥 lock,执行语句块,然后释放 lock. 持有 lock 时,持有 lock 的线程可以再次获取并释放 lock. 阻止任何其他线程获取 lock 并 ...
- python运维开发常用模块(7)web探测模块pycurl
1.模块介绍 pycurl(http://pycurl.sourceforge.net)是一个用C语言写的libcurl Python实现,功能非常强大,支持的操作协议有FTP.HTTP.HTTPS. ...
- Jupyter Notebook使用
不论你是刚开始学 Python,还是正在啃数据分析的骨头,对你来说,不断在各种命令行窗口和编辑器里切来切去,或者不断打开各种窗口查看 matplotlib 的输出之类的繁琐操作,一定是家常便饭了.哎呀 ...
- LeetCode 283:移动零 Move Zeroes
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. Given an array nums, write a function to move all 0' ...