CF1012D AB-Strings 题解
首先,我们容易发现连续的\(a\)和\(b\)可以缩为\(1\)个,那么我们相当于让形如\(ababababa……baba\)的匹配,我们发现一次最多消掉两个位置,考虑尽可能多的消掉\(2\)个位置,那么我们需要两个串的长度尽可能的平均,也就可以让边长长的多给边长短的一些,比如把交换\(a\)和空改为交换\(aba\)和空,随便玩玩就基本可以看出来方法,然后每种情况分别考虑,进行一些些小分讨,我只分了17种情况,加上调试输出仅仅只有5kb代码,简单易行,优势在我。
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn=500010;
int tp1,tp2,lst1,lst2,tp,lin;
pair<int,int>st1[maxn],st2[maxn],ci[maxn];
string a,b;
signed main(){
cin>>a>>b;
for(int i=a.size()-1;i>=0;i--){
if(a[i]-'a'==lst1){
lst2++;
}
else{
if(lst2){
tp1++;
st1[tp1]={lst1,lst2};
}
lst2=1;
lst1^=1;
}
}
if(lst2){
tp1++;
st1[tp1]={lst1,lst2};
}
lst1=lst2=0;
for(int i=b.size()-1;i>=0;i--){
if(b[i]-'a'==lst1){
lst2++;
}
else{
if(lst2){
tp2++;
st2[tp2]={lst1,lst2};
}
lst2=1;
lst1^=1;
}
}
if(lst2){
tp2++;
st2[tp2]={lst1,lst2};
}
while(tp1!=1||tp2!=1){
tp++;
if(st1[tp1].first==st2[tp2].first){
if(tp1==1){
if(tp2==2){
ci[tp].first=0;
ci[tp].second=st2[tp2].second;
st1[tp1].second+=st2[tp2].second;
tp2--;
}
else if(tp2==3||tp2==4){
ci[tp].first=st1[tp1].second;
ci[tp].second=st2[tp2].second+st2[tp2-1].second;
st2[tp2-2].second+=st1[tp1].second;
st1[tp1]=st2[tp2-1];
tp1++;
st1[tp1]=st2[tp2];
tp2-=2;
}
else{
ci[tp].first=0;
ci[tp].second=st2[tp2].second+st2[tp2-1].second+st2[tp2-2].second;
lin=st1[tp1].second;
tp1+=2;
st1[tp1]=st2[tp2];
st1[tp1-1]=st2[tp2-1];
st1[tp1-2]=st2[tp2-2];
st1[tp1-2].second+=lin;
tp2-=3;
}
}
else if(tp2==1){
if(tp1==2){
ci[tp].first=st1[tp1].second;
ci[tp].second=0;
st2[tp2].second+=st1[tp1].second;
tp1--;
}
else if(tp1==3||tp1==4){
ci[tp].first=st1[tp1].second+st1[tp1-1].second;
ci[tp].second=st2[tp2].second;
st1[tp1-2].second+=st2[tp2].second;
st2[tp2]=st1[tp1-1];
tp2++;
st2[tp2]=st1[tp1];
tp1-=2;
}
else{
ci[tp].first=st1[tp1].second+st1[tp1-1].second+st1[tp1-2].second;
ci[tp].second=0;
lin=st2[tp2].second;
tp2+=2;
st2[tp2]=st1[tp1];
st2[tp2-1]=st1[tp1-1];
st2[tp2-2]=st1[tp1-2];
st2[tp2-2].second+=lin;
tp1-=3;
}
}
else{
if(tp1>2&&tp1>=tp2){
ci[tp].first=st1[tp1].second+st1[tp1-1].second;
ci[tp].second=st2[tp2].second;
st1[tp1-2].second+=st2[tp2].second;
st2[tp2-1].second+=st1[tp1-1].second;
st2[tp2].second=st1[tp1].second;
tp1-=2;
}
else if(tp2>2&&tp2>=tp1){
ci[tp].first=st1[tp1].second;
ci[tp].second=st2[tp2].second+st2[tp2-1].second;
st2[tp2-2].second+=st1[tp1].second;
st1[tp1-1].second+=st2[tp2-1].second;
st1[tp1].second=st2[tp2].second;
tp2-=2;
}
else{
if(tp1>tp2){
ci[tp].first=st1[tp1].second;
ci[tp].second=0;
st2[tp2].second+=st1[tp1].second;
tp1--;
}
else{
ci[tp].first=0;
ci[tp].second=st2[tp2].second;
st1[tp1].second+=st2[tp2].second;
tp2--;
}
}
}
}
else{
if(tp1>4&&tp2==1){
ci[tp].first=st1[tp1].second+st1[tp1-1].second+st1[tp1-2].second;
ci[tp].second=st2[tp2].second;
st1[tp1-3].second+=st2[tp2].second;
st2[tp2]=st1[tp1-2];
tp2++;
st2[tp2]=st1[tp1-1];
tp2++;
st2[tp2]=st1[tp1];
tp1-=3;
}
else if(tp1>4&&tp1>=tp2){
ci[tp].first=st1[tp1].second+st1[tp1-1].second+st1[tp1-2].second;
ci[tp].second=st2[tp2].second;
st1[tp1-3].second+=st2[tp2].second;
st2[tp2-1].second+=st1[tp1-2].second;
st2[tp2]=st1[tp1-1];
tp2++;
st2[tp2]=st1[tp1];
tp1-=3;
}
else if(tp2>4&&tp1==1){
ci[tp].first=st1[tp1].second;
ci[tp].second=st2[tp2].second+st2[tp2-1].second+st2[tp2-2].second;
st2[tp2-3].second+=st1[tp1].second;
st1[tp1]=st2[tp2-2];
tp1++;
st1[tp1]=st2[tp2-1];
tp1++;
st1[tp1]=st2[tp2];
tp2-=3;
}
else if(tp2>4&&tp2>=tp1){
ci[tp].first=st1[tp1].second;
ci[tp].second=st2[tp2].second+st2[tp2-1].second+st2[tp2-2].second;
st2[tp2-3].second+=st1[tp1].second;
st1[tp1-1].second+=st2[tp2-2].second;
st1[tp1]=st2[tp2-1];
tp1++;
st1[tp1]=st2[tp2];
tp2-=3;
}
else if(tp1>1&&tp2>1){
ci[tp].first=st1[tp1].second;
ci[tp].second=st2[tp2].second;
st1[tp1-1].second+=st2[tp2].second;
st2[tp2-1].second+=st1[tp1].second;
tp1--,tp2--;
}
else if(tp1>1){
ci[tp].first=st1[tp1].second;
ci[tp].second=st2[tp2].second;
st1[tp1-1].second+=st2[tp2].second;
st2[tp2]=st1[tp1];
tp1--;
}
else{
ci[tp].first=st1[tp1].second;
ci[tp].second=st2[tp2].second;
st2[tp2-1].second+=st1[tp1].second;
st1[tp1]=st2[tp2];
tp2--;
}
}
// cout<<tp1<<'\n';
// for(int i=tp1;i>=1;i--){
// cout<<st1[i].first<<' '<<st1[i].second<<'\n';
// }
// cout<<tp2<<'\n';
// for(int i=tp2;i>=1;i--){
// cout<<st2[i].first<<' '<<st2[i].second<<'\n';
// }
// cout<<'\n';
}
cout<<tp<<'\n';
for(int i=1;i<=tp;i++){
cout<<ci[i].first<<' '<<ci[i].second<<'\n';
}
return 0;
}