传送门

题意:

给出一个只含前\(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的更多相关文章

  1. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  2. Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  3. Codeforces Round #501 (Div. 3) F. Bracket Substring

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  4. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  5. Codeforces Round #590 (Div. 3) Editorial

    Codeforces Round #590 (Div. 3) Editorial 题目链接 官方题解 不要因为走得太远,就忘记为什么出发! Problem A 题目大意:商店有n件商品,每件商品有不同 ...

  6. Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)

    题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...

  7. Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)

    题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...

  8. 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 ...

  9. Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)

    题目地址:http://codeforces.com/contest/474/problem/F 由题意可知,最后能够留下来的一定是区间最小gcd. 那就转化成了该区间内与区间最小gcd数相等的个数. ...

随机推荐

  1. ACM-冒泡排序

    将多组输入数据进行冒泡排序,并去除相同的数据 #include <iostream> #include <vector> using namespace std; void R ...

  2. anaconda换源及创建虚拟环境

    0x01:换源,依次输入一下两条命令 conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/fr ...

  3. python3.5.3rc1学习四:类

    class calculator: def add(x,y): return x + y print(added) def sub(x,y): return x - y print(sub) def ...

  4. Badboy安装和简单介绍

    参考链接:https://www.cnblogs.com/baoziluo/p/7907964.html

  5. 《淘宝数据库OceanBase SQL编译器部分 源码阅读--解析SQL语法树》

    淘宝数据库OceanBase SQL编译器部分 源码阅读--解析SQL语法树   曾经的学渣 2014-06-05 18:38:00 浏览1455 云数据库Oceanbase   OceanBase是 ...

  6. zz自动驾驶多传感器感知的探索1

    Pony.ai 在多传感器感知上积累了很多的经验,尤其是今年年初在卡车上开始了新的尝试.我们有不同的传感器配置,以及不同的场景,对多传感器融合的一些新的挑战,有了更深刻的认识,今天把这些经验,总结一下 ...

  7. Velocity与Jsp、Freemarker的对比

    在java领域,表现层技术主要有三种:jsp.freemarker.velocity.jsp是大家最熟悉的技术 优点: 1.功能强大,可以写java代码 2.支持jsp标签(jsp tag) 3.支持 ...

  8. layer之关闭弹窗

    一. layer关闭弹出层方法1-1) 先获取某个弹出层的 index var index = layer.open();   var index = layer.alert(); var index ...

  9. 用scratch写了一个win98 界面模拟

    scratch模拟win98 玩耍,放在网上了https://kada.163.com/project/4216461-3515165.htm

  10. Token认证,如何快速方便获取用户信息

    背景 我们有一个Web项目,这个项目提供了很多的Rest API.也做了权限控制,访问API的请求必须要带上事先认证后获取的Token才可以. 认证的话就在Filter中进行的,会获取请求的Token ...