这个代码在干什么?
  • 板块灌水区
  • 楼主lucy2012
  • 当前回复9
  • 已保存回复9
  • 发布时间2024/9/15 19:28
  • 上次更新2024/9/15 21:58:44
查看原帖
这个代码在干什么?
1252442
lucy2012楼主2024/9/15 19:28
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
char s[200005];
int cnt[10],ed[200005][10];
int main(){
    scanf("%s",s+1);
    int l=strlen(s+1);
    memset(cnt,0,sizeof(cnt));
    for (int i=1; i<=l; i++)
        cnt[s[i]-'0']++;
    for (int j=0; j<10; j++)
        ed[l+1][j]=l+1;
    for (int i=l; i>=1; i--){
        for (int j=0; j<=9; j++)
            ed[i][j]=ed[i+1][j];
        ed[i][s[i]-'0']=i;
    }
    int res=0;
    for (int i=0; i<=9; i++){
        if (cnt[i]==0) continue;
        res=max(res,cnt[i]);
        for (int j=0; j<=9; j++){
            if (cnt[j]==0) continue;
            if (i!=j){
                int st=ed[1][i],tot=0,flag=1;
                while (st<=l){
                    tot++;
                    if (flag) {
                        if (ed[st+1][j]>l) break;
                        st=ed[st+1][j];
                    }
                    else {
                        if (ed[st+1][i]>l) break;
                        st=ed[st+1][i];
                    }
                    flag=flag^1;
                }
                if (tot%2==1) tot--;
                res=max(res,tot);
            }
        }
    }
    printf("%d\n",l-res);
}
2024/9/15 19:28
加载中...