跳到正文

题解

题解 CF1422E 【Minlexes】

ycyaw约 4 分钟
原文
文章信息与版本

文章信息

发布
2020年10月21日
收录
2026年7月15日
更新
2026年11月12日
篇幅
约 4 分钟
获赞
5
题目
CF1422E

版本

  1. v1当前

先考虑最朴素的dpdp,fif_i表示第ii位为开头的后缀答案字符串

倒着做,分两种情况转移:

  1. 若si=si+1s_i=s_{i+1},考虑删或不删,fi=min(fi+2,si+si+fi+2)f_i=min(f_{i+2},s_i+s_i+f_{i+2})

  2. 否则删不了,fi=si+fi+1f_i=s_i+f_{i+1}

其中minmin表示两个字符串字典序比较,加号表示字符串拼接

比较麻烦的是第一种情况,我们先令fi=fi+2f_i=f_{i+2},定义相同的两个字符都为chch。

若fif_i为空,显然删掉更优

否则,若ch<fich<f_i的第一位,显然不删更优;若ch=fich=f_i的第一位,我们维护每个字符串的最前面的不相同的两个相邻字符的大小关系(如fffgfffg,最前面的不相同的两个字符是fgfg),若递增(如例子中f<gf<g),则不删更优,否则删了更优。

建立一个结构体来处理题目需要的输出格式,代码实现还是很简洁的。

Code Below:Code\ Below:

C++
#include<bits/stdc++.h>
#define ts cout<<"ok"<<endl
#define ll long long
#define hh puts("")
#define pc putchar
//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
//char buf[1<<21],*p1=buf,*p2=buf;
using namespace std;
const int N=100005;
struct node{
    string pre,suf;
    int len,inc;
    node(){
        pre="",suf="";
        len=inc=0;
    }
    void ins(char ch){
        if(pre.size()&&ch!=pre[0]) inc=(ch<pre[0]);
        len++;
        if(suf.size()<2) suf=ch+suf;
        pre=ch+pre;
        if(pre.size()>10) pre.pop_back();
    }
};
node ans[N];
string s;
inline int read(){
    int ret=0,ff=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-') ff=-1;ch=getchar();}
    while(isdigit(ch)){ret=ret*10+(ch^48);ch=getchar();}
    return ret*ff;
}
void write(int x){if(x<0){x=-x,pc('-');}if(x>9) write(x/10);pc(x%10+48);}
void writeln(int x){write(x),hh;}
void writesp(int x){write(x),pc(' ');}
signed main(){
    cin>>s;
    for(int i=s.size()-1;i>=0;i--){
        if(i+1<s.size()&&s[i]==s[i+1]){
            ans[i]=ans[i+2];
            if(ans[i].len&&(s[i]<ans[i].pre[0]||(s[i]==ans[i].pre[0]&&ans[i].inc))){
                ans[i].ins(s[i]);
                ans[i].ins(s[i]);
            }
        }
        else{
            ans[i]=ans[i+1];
            ans[i].ins(s[i]);
        }
    }
    for(int i=0;i<s.size();i++){
        writesp(ans[i].len);
        if(ans[i].len<=10) cout<<ans[i].pre<<endl;
        else cout<<ans[i].pre.substr(0,5)+"..."+ans[i].suf<<endl;
    }
    return 0;
}
原文