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数相等的个数. ...
随机推荐
- 初学JavaScript正则表达式(二)
正则表达式的实例化与标识符 字面量: var reg = /\bis\b/g // \b--字符边界 g全文搜索 查找单词为is的字符 He is a boy. IS He? 构造函数: var re ...
- 《Verilog数字系统设计教程(第2版).pdf》
Verilog数字系统设计教程(第2版).pdf https://github.com/shigh1005/pdf_book <Linux设备驱动开发详解:基于最新的Linux 4.0内核.pd ...
- 学习:反调试之IsDebuggerPresent
前言:一个反调试IsDebuggerPresent的CreackMe IsDebuggerPresent函数的了解: IsDebuggerPresent 作用 确定调用进程是否由用户模式的调试器调试. ...
- C# git忽略文件 (.gitignore )
# Visual Studio # User-specific files *.suo *.user *.userosscache *.sln.docstates # User-specific fi ...
- Vue 事件的基本使用与语法差异
"v-on:"可以简写为"@" "click"单击 "dblclick"双加 代码: <!doctype html ...
- springcloud源码分析(一)之采用redis实现注册中心
注册中心 在分布式架构中注册中心起到了管理各种服务功能包括服务的注册.发现.熔断.负载.降级等功能,在分布式架构中起到了不可替代的作用.常见的注册中心有eureka,zookeeper等等,在spri ...
- JavaScript查找两个数组的相同元素和相差元素
let intersection = a.filter(v => b.includes(v)) 返回交集数组 let difference = a.concat(b).filter(v => ...
- ES6-Symbol.iterator 迭代器
一个数据结构只要部署了Symbol.iterator属性就能使用 for...of遍历 与 ...运算符 操作 Object身上没有Symbol.iterator,当直接使用时会报错 let obj ...
- golang实战--家庭收支记账软件(面向过程)
1.开发流程 2.目标 模拟实现一个基于文本界面的(家庭记账软件) : 初步掌握编程技巧和调试技巧: 主要包含以下知识点:局部变量和基本数据类型.循环语句.分支语句.简单屏幕格式输出.面向对象编程: ...
- 响应国家号召 1+X 证书 Web 前端开发考试模拟题
1+x证书Web前端开发初级理论考试样题2019 http://blog.zh66.club/index.php/archives/149/ 1+x证书Web前端开发初级实操考试样题2019 http ...