CF1234F Yet Another Substring Reverse(状压dp)

Meaning of the questions:

  Given a string of length n $ (n \ leq 1e6) $, from the letter at (20 letters), one operation can be performed, a certain period reverses the string, after the operation, not all letters substring the same longest length for the substring.

Problem-solving ideas:

  A total of 20 letters instead of 26 letters, almost like the words written on the face of pressure. Easy to see that, for any two non-adjacent sub-strings by reversing its operation spliced ​​together, the task becomes the same letter are not find two sub-string, its length and the maximum.

  For $ dp [i] $, it represents all the length of the longest substring in the absence of the letter 0 entries. Thus, it is possible to obtain transfer equation, $ dp [i | (1 << j)] = max (dp [i], dp [i | (1 << j)]) $, and finally, just enumerate i, plus on $ dp [(1 << 20) -1-it] $ max can be taken.

  And finally paste the code

#include<bits/stdc++.h>
 
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int,int> pii;
#define rep(i,x,y) for(int i=(x);i<(y);i++)
#define rept(i,x,y) for(int i=(x);i<=(y);i++)
#define per(i,x,y) for(int i=x;i>=y;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define de(x) cout<< #x<<" = "<<x<<endl
#define dd(x) cout<< #x<<" = "<<x<<" "
#define mes(a,b) memset(a,b,sizeof a)
const int inf= 0x3f3f3f3f;
 
string s;
int dp[1<<20];
 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    mes(dp,0);
    cin>>s;
    int len=s.size();
    rep(i,0,len) 
        0with =int
    {;
        rep(j,i,len)
        {
            if( val&(1<<(s[j]-'a')) )
            {
                //dd(i);de(j);//dd(val);de((1<<(s[j]-'a')));
                break;
            }
            val|=(1<<(s[j]-'a'));
            dp[val]=j-i+1;
        }
    }
    rep(i,0,(1<<20))
        rep(j,0,20)
        {
            if(i&(1<<j)) continue;
            dp[i|(1<<j)]=max(dp[i],dp[i|(1<<j)]);
        }
    int ans=0;
    rep(i,0,(1<<20))
    {
        ans=max(ans,dp[i]+dp[((1<<20)-1)^i]);
    }
    cout<<ans<<"\n";
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/FZUzyz/p/12397864.html