QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#244560 | #4482. BBQ | ucup-team203# | AC ✓ | 135ms | 8468kb | C++20 | 2.1kb | 2023-11-09 11:48:29 | 2023-11-09 11:48:29 |
Judging History
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