我叫王大锤,是一家出版社的编辑。我负责校对投稿来的英文稿件,这份工作非常烦人,因为每天都要去修正无数的拼写错误。但是,优秀的人总能在平凡的工作中发现真理。我发现一个发现拼写错误的捷径:
- 三个同样的字母连在一起,一定是拼写错误,去掉一个的就好啦**:比如 helllo -> hello**
- 两对一样的字母(AABB型)连在一起,一定是拼写错误,去掉第二对的一个字母就好啦:比如 helloo -> hello
- 上面的规则优先“从左到右”匹配,即如果是AABBCC,虽然AABB和BBCC都是错误拼写,应该优先考虑修复AABB,结果为AABCC
输入描述:
第一行包括一个数字N,表示本次用例包括多少个待校验的字符串。
后面跟随N行,每行为一个待校验的字符串。
输出描述:
N行,每行包括一个被修复后的字符串。
输入例子1:
2
helloo
wooooooow
输出例子1:
hello
woow
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Solution{
public:
string jiaozheng(string& str){
if(str==" " || str.size()==0)
return "";
vector<char> res;
res.push_back(' ');
int flag=0;//用于标记属于那种情况。
int j=0;//用于记录res数组的位置
for(int i=0; i<str.size();i++){
char temp=str[i];
if(temp!=res[j]){
if(flag==0){
res.push_back(temp);
j++;
} else if (flag==1){
res.push_back(temp);
j++;
flag=2;
}else if(flag==2){
res.push_back(temp);
j++;
flag=0;
}
}else{
if(flag==0){
res.push_back(temp);
j++;
flag=1;
}
}
}
string strs;
for(int i=1; i<res.size();i++){
strs=strs+res[i];
}
return strs;
}
};
int main() {
int N;
cin>>N;
vector<string> ves;
Solution p;
for(int i=0;i<N;i++){
string temp;
cin>> temp;
ves.push_back(p.jiaozheng(temp));
}
for(int i=0; i<ves.size();i++){
cout<<ves[i]<<endl;
}
return 0;
}