注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Minary_Acdream

http://f10.moe/

 
 
 

日志

 
 

CodeForces----A.Dubstep  

2012-07-24 10:35:05|  分类: CodeForces |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

A. Dubstep
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya works as a DJ in the best Berland nightclub, and he often uses dubstep music in his performance. Recently, he has decided to take a couple of old songs and make dubstep remixes from them.

Let's assume that a song consists of some number of words. To make the dubstep remix of this song, Vasya inserts a certain number of words "WUB" before the first word of the song (the number may be zero), after the last word (the number may be zero), and between words (at least one between any pair of neighbouring words), and then the boy glues together all the words, including "WUB", in one string and plays the song at the club.

For example, a song with words "I AM X" can transform into a dubstep remix as "WUBWUBIWUBAMWUBWUBX" and cannot transform into "WUBWUBIAMWUBX".

Recently, Petya has heard Vasya's new dubstep track, but since he isn't into modern music, he decided to find out what was the initial song that Vasya remixed. Help Petya restore the original song.

Input

The input consists of a single non-empty string, consisting only of uppercase English letters, the string's length doesn't exceed 200characters. 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.

Output

Print the words of the initial song that Vasya used to make a dubsteb remix. Separate the words with a space.

Sample test(s)
input
WUBWUBABCWUB
output
ABC 
input
WUBWEWUBAREWUBWUBTHEWUBCHAMPIONSWUBMYWUBFRIENDWUB
output
WE ARE THE CHAMPIONS MY FRIEND 
Note

In the first sample: "WUBWUBABCWUB" = "WUB" + "WUB" + "ABC" + "WUB". That means that the song originally consisted of a single word "ABC", and all words "WUB" were added by Vasya.

In the second sample Vasya added a single word "WUB" between all neighbouring words, in the beginning and in the end, except for words "ARE" and "THE" — between them Vasya added two "WUB".


   对于字符串分割不怎么熟练,就卡了很久= =

   下面是用STL 来做的:

这题需要注意的是连续的WUB。

#include<iostream>
#include<vector>
#include<string>
using namespace std;

int main()
{
string str,_str = "WUB";
cin>>str;

string::size_type pot;
str += _str;
int size = str.size();
vector<string>result;

for(int i = 0;i < size;i ++)
{
pot = str.find(_str,i);
if(pot < size)
{
string s = str.substr(i,pot - i);
if(s != "") result.push_back(s); //判断是否有连续
i = pot + _str.size() - 1;
}
}

for(int i = 0;i < result.size() - 1;i ++)
cout<<result[i]<<" ";
cout<<result[result.size() - 1]<<endl;

return 0;
}

由这题可以拓展开用字符串pattern来分割字符串str。

涉及到string类的两个函数find和substr:
1、find函数
原型:size_t find ( const string& str, size_t pos = 0 ) const;
功能:查找子字符串第一次出现的位置。
参数说明:str为子字符串,pos为初始查找位置。
返回值:找到的话返回第一次出现的位置,否则返回string::npos 

2、substr函数
原型:string substr ( size_t pos = 0, size_t n = npos ) const;
功能:获得子字符串。
参数说明:pos为起始位置(默认为0),n为结束位置(默认为npos)
返回值:子字符串 

代码如下:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
//字符串分割函数
vector<string> split(string str,string pattern)
{
string::size_type pos;
vector<string> result;
str+=pattern;//扩展字符串以方便操作
int size=str.size();

for(int i=0; i<size; i++)
{
pos=str.find(pattern,i);
if(pos<size)
{
string s=str.substr(i,pos-i);
result.push_back(s);
i=pos+pattern.size()-1;
}
}
return result;
}

int main()
{
string str;
cout<<"Please input str:"<<endl;
getline(cin,str);
string pattern;
cout<<"Please input pattern:"<<endl;
getline(cin,pattern);//用于获取含空格的字符串
vector<string> result=split(str,pattern);
cout<<"The result:"<<endl;
for(int i=0; i<result.size(); i++)
{
if(result[i] != "")
cout<<result[i]<<" ";
}
cout<<endl;
for(int i=0; i<result.size(); i++)
{
cout<<result[i];
}

cin.get();
cin.get();
return 0;
}


如果仅仅需要是是用字符分割,可以借用函数。我总结了下面两种

方法一:

可以用getlin进行输入。

       原型:getline ( istream &is , string &str , char delim );

                 getline ( istream& , string& );

用法(举例):

strint str;

getline(cin,str,'#');

//上面即是用‘#’来分割输入的字符串。


方法二:

strtok函数分割。

    原型: char *strtok(char *str, const char *delim);

       用法(举例):

char s[] = "Golden Global View,disk * desk";

const char *d = " ,*"; 

char *p;

p = strtok(s,d);

  //上面即是用',','*',' ';来分割输入字符串


其他: 
replace函数:


  basic_string &replace( size_type index, size_type num, const basic_string &str );
basic_string &replace( size_type index1, size_type num1, const basic_string &str, size_type index2,
size_type num2 );
basic_string &replace( size_type index, size_type num, const char *str );
basic_string &replace( size_type index, size_type num1, const char *str, size_type num2 );
basic_string &replace( size_type index, size_type num1, size_type num2, char ch );
basic_string &replace( iterator start, iterator end, const basic_string &str );
basic_string &replace( iterator start, iterator end, const char *str );
basic_string &replace( iterator start, iterator end, const char *str, size_type num );
basic_string &replace( iterator start, iterator end, size_type num, char ch );

  • 用str中的num个字符替换本字符串中的字符,从index开始
  • 用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,最多num1个字符
  • 用str中的num个字符(从index开始)替换本字符串中的字符
  • 用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,num1个字符
  • 用num2个ch字符替换本字符串中的字符,从index开始
  • 用str中的字符替换本字符串中的字符,迭代器start和end指示范围
  • 用str中的num个字符替换本字符串中的内容,迭代器start和end指示范围,
  • 用num个ch字符替换本字符串中的内容,迭代器start和end指示范围.
举例:
    string s = "They say he carved it himself...from a BIGGER spoon";
    string s2 = "find your soul-mate, Homer.";      s.replace( 32, s2.length(), s2 );      cout << s << endl; 
今天又有去好好思考过,字符串替换字符串的问题,借鉴了某位牛的代码后,整理了个函数:


void string_replace(string & strBig, const string & strsrc, const string &strdst)
{
string::size_type pos=0;
string::size_type srclen=strsrc.size();
string::size_type dstlen=strdst.size();
while( (pos=strBig.find(strsrc, pos)) != string::npos)
{
strBig.replace(pos, srclen, strdst);
pos += dstlen;
}
}




find函数:
 find 查找 
rfind 反向查找 
find_first_of 查找包含子串中的任何字符,返回第一个位置 
find_first_not_of 查找不包含子串中的任何字符,返回第一个位置
find_last_of 查找包含子串中的任何字符,返回最后一个位置 
find_last_not_of 查找不包含子串中的任何字符,返回最后一个位置
以上函数都是被重载了4次,以下是以find_first_of 函数为例说明他们的参数,其他函数和其参数一样,也就是说总共有24个函数:
size_type find_first_of(const basic_string& s, size_type pos = 0)
size_type find_first_of(const charT* s, size_type pos, size_type n)
size_type find_first_of(const charT* s, size_type pos = 0)
size_type find_first_of(charT c, size_type pos = 0)
       这些函数在查找中如果没有找到要找的字符,那么返回 string::npos
      find 和 rfind 都还比较容易理解,一个是正向匹配,一个是逆向匹配,后面的参数pos都是用来指定起始查找位置。对于find_first_of 和find_last_of 就不是那么好理解。

find_first_of 是给定一个要查找的字符集,找到这个字符集中任何一个字符所在字符串中第一个位置。
        给个例子理解:


#include <string>
#include <iostream>
using namespace std;

int main()
{

string strinfo="//---Hello Word!......------";
string strset="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int first = strinfo.find_first_of(strset);
cout<<first<<endl;
if(first == string::npos)
{
cout<<"not find any characters"<<endl;
return -1;
}
int last = strinfo.find_last_of(strset);
cout<<last<<endl;
if(last == string::npos)
{
cout<<"not find any characters"<<endl;
return -1;
}
cout << strinfo.substr(first, last - first + 1)<<endl;

}



运行结果:
5
14
 Hello Word

  评论这张
 
阅读(497)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018