题解:P10368 「LAOI-4」Colors
Chu_awa_xing · · 题解
P10368题解
这题可以直接用字符串进行修改,因为字符串相加就是前者拼接上了后者。其次这里有一个优化,就是如果一次操作后,字符串没有变化,那么剩下的操作就没必要执行了,直接输出答案即可。
代码
#include<bits/stdc++.h>
using namespace std;
string s;
int id,T;
inline int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0' && ch<='9')
x=x*10+ch-'0',ch=getchar();
return x*f;
}
string f(string s){
string tmp="";
for(int i=0;i<s.length();i++){
if((!i)||i==s.length()-1){
tmp+=s[i];
}
else if(s[i-1]!=s[i+1]){
tmp+=s[i];
}
}
return tmp;
}
signed main(){
T=read(),id=read();
while(T--){
long long n,k;
n=read(),k=read();
cin>>s;
while(k--){
string s0=f(s);
if(s==s0){
break;
}
else s=s0;
}
cout<<s<<"\n";
}
}