QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#244560#4482. BBQucup-team203#AC ✓135ms8468kbC++202.1kb2023-11-09 11:48:292023-11-09 11:48:29

Judging History

你现在查看的是最新测评结果

  • [2023-11-09 11:48:29]
  • 评测
  • 测评结果:AC
  • 用时:135ms
  • 内存:8468kb
  • [2023-11-09 11:48:29]
  • 提交

answer

#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<string.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,int> PIi;
const int maxn=1e6+10;
const ll inf = 0x3f3f3f3f3f3f3f3f;
//10:22 50

// #include<map>
// vector<string> v;
// map<string,int> lib;
// void Dijkstra(){

// }

char s[maxn];
int dp[maxn];

int main()
{
    // rep(i,1,8){
    //     Dfs(0,0,i);
    // }
    int _;  cin>>_;
    while(_--){
        scanf("%s",s+1);
        int n = strlen(s+1);
        if(n<=2){
            printf("%d\n",n);
            continue;
        }

        dp[0]=0,dp[1]=1,dp[2]=2;
        if(s[1]!=s[2]&&s[1]!=s[3]&&s[2]!=s[3]) dp[3]=2;
        else dp[3]=1;
        rep(i,4,n){
            dp[i]=min(dp[i-1]+1,dp[i-2]+2);
            if(s[i-2]!=s[i-1]&&s[i-2]!=s[i]&&s[i-1]!=s[i]) dp[i]=min(dp[i-3]+2,dp[i]);
            else dp[i]=min(dp[i-3]+1,dp[i]);

            int cnt=(s[i-3]!=s[i]?1:0)+(s[i-2]!=s[i-1]?1:0);
            dp[i]=min(dp[i-4]+cnt,dp[i]);

            if(i>=5){
                int cnt=s[i-4]!=s[i]?1:0;
                cnt+=min({s[i-3]!=s[i-2]?1:0,s[i-3]!=s[i-1]?1:0,s[i-2]!=s[i-1]?1:0});
                dp[i]=min(dp[i-5]+1+cnt,dp[i]);
            }
            if(i>=6){
                int cnt=s[i-5]!=s[i]?1:0;
                cnt+=min({s[i-4]!=s[i-3]?1:0,s[i-4]!=s[i-2]?1:0,s[i-4]!=s[i-1]?1:0,
                          s[i-3]!=s[i-2]?1:0,s[i-3]!=s[i-1]?1:0,s[i-2]!=s[i-1]?1:0 });
                dp[i]=min(dp[i-6]+2+cnt,dp[i]);
            }
            if(i>=7){
                int cnt=s[i-6]!=s[i]?1:0;
                cnt+=min({s[i-5]!=s[i-4]?1:0,s[i-5]!=s[i-3]?1:0,s[i-5]!=s[i-2]?1:0,s[i-5]!=s[i-1]?1:0,
                        s[i-4]!=s[i-3]?1:0,s[i-4]!=s[i-2]?1:0,s[i-4]!=s[i-1]?1:0,
                          s[i-3]!=s[i-2]?1:0,s[i-3]!=s[i-1]?1:0,s[i-2]!=s[i-1]?1:0 });
                dp[i]=min(dp[i-7]+3+cnt,dp[i]);
            }
        }
        cout<<dp[n]<<endl;
    }
    fflush(stdin);
    getchar();
}
/*
1
abbba
acbbea
acbdcea
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 135ms
memory: 8468kb

input:

11
a
hvusknygbjxrwidvexsqopbbouvukgpipagvpmravchjrswtthzrambhmueczcclgltvlbvjzwxcjvwtvnzmxakzrwklrhlrsymiraoyfwaakhjssdggbwadpduyuqidyviwsjfrdbwjvbrvgomwktpgvhbaoluhhdgvqotajhwihkrpnueuqahvaqkzodquklqagjmbccgpkiirxsqbnmwmimjvyrwvkknxbwcqifblbzgylgffpojvjvjyxueopjaenqobfyynfruhetamiryptcgemjlwhibbtaq...

output:

1
441151
441261
441604
203781
203409
203508
138252
138404
138479
304092

result:

ok 11 lines