传送门
文章目录
- 题意:
- 思路:
题意:
思路:
之前做过类似的题,翻转一个字串相当于将任意两个不相交的串连在一起。再一看字符集≤20\le20≤20,那就是铁子集dpdpdp了。
定义f[i]f[i]f[i]表示状态为iii的串的长度,那么预处理的时候需要预处理出来所有相邻且不含相同字符的串,这个显然可以在n∗20n*20n∗20的复杂度内做到。
之后就是套路了,我们将子集信息向上传,让后答案就是f[i]+f[((1<<20)−1)xori]f[i]+f[((1<<20)-1) \ \ xor\ \ i ]f[i]+f[((1<<20)−1) xor i]。
// Problem: F. Yet Another Substring Reverse
// Contest: Codeforces - Codeforces Round #590 (Div. 3)
// URL: https://codeforces.com/contest/1234/problem/F
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=5000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int n;
char s[N];
int f[N],c[30];
vector<int>ans;int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);cin>>(s+1); n=strlen(s+1);for(int i=1;i<=n;i++) {int now=0;for (int j=0;j<20&&i+j<=n;j++) {int ch=s[i+j]-'a';if (now&(1<<ch)) {break;}now|=(1<<ch);f[now]=j+1;}}//for(int i=0;i<20;i++) if(f[i]!=__builtin_popcount(i)&&f[i]) cout<<i<<' '<<f[i]<<endl;for(int j=0;j<20;j++) {for(int i=0;i<1<<20;i++) {if(!((i>>j)&1)) {f[i^(1<<j)]=max(f[i^(1<<j)],f[i]);}}}int ans=0;for(int i=0;i<1<<20;i++) {ans=max(ans,f[i]+f[((1<<20)-1)^i]);}printf("%d\n",ans);return 0;
}
/**/