标签:复杂度 ++ problem for 比较 open syn size air
题意:
给出一个只含前\(20\)个字符的字符串,现在可以选择一段区间进行翻转,问区间中字符各不相同时,最长长度为多少。
思路:
其实只要发现满足条件的区间不超过\(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
标签:复杂度 ++ problem for 比较 open syn size air
原文地址:https://www.cnblogs.com/heyuhhh/p/11618860.html