题目链接:
http://codeforces.com/problemset/problem/208/A
A. Dubstep
memory limit per test:256 megabytes
输入
The input consists of a single non-empty string, consisting only of uppercase English letters, the string's length doesn't exceed 200 characters. It is guaranteed that before Vasya remixed the song, no word contained substring "WUB" in it; Vasya didn't change the word order. It is also guaranteed that initially the song had at least one word.
输出
Print the words of the initial song that Vasya used to make a dubsteb remix. Separate the words with a space.
样例
sample input
WUBWUBABCWUBsample output
ABCsample input
WUBWEWUBAREWUBWUBTHEWUBCHAMPIONSWUBMYWUBFRIENDWUBsample output
WE ARE THE CHAMPIONS MY FRIEND
题意
给你一串字母,求去掉分割标记"WUB"之后的句子。
题解
开一个缓存,直接模拟,匹配一个"WUB"之后把缓存里的值取出来,清空缓存然后跳过这个“WUB"继续做。
注意事项:退出循环之后要检查一下缓存里面是不是有东西。
(数据很小,用c++的string类做比较方便。)
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<string>
using namespace std;
const int maxn=1e6+10;
typedef long long LL;
string str;
vector<string> ans;
int n;
int main(){
cin>>str;
string tmp="";
for(int i=0;i<str.length();i++){
if(i+2<str.length()&&str[i]=='W'&&str[i+1]=='U'&&str[i+2]=='B'){
if(tmp!=""){
ans.push_back(tmp);
}
tmp="";
i+=2;
}else{
tmp+=str[i];
}
}
if(tmp!="") ans.push_back(tmp);
for(int i=0;i<ans.size()-1;i++) cout<<ans[i]<<' ';
cout<<ans[ans.size()-1]<<endl;
return 0;
}