HDU 5007-Post Robot(字符串)
2014-09-15 08:29
453 查看
[align=left]Problem Description[/align]
DT is a big fan of digital products. He writes posts about technological products almost everyday in his blog.
But there is such few comments of his posts that he feels depressed all the day. As his best friend and an excellent programmer, DT asked you to help make his blog look more popular. He is so warm that you have no idea how to refuse. But you are unwilling to
read all of his boring posts word by word. So you decided to write a script to comment below his posts automatically.
After observation, you found words “Apple” appear everywhere in his posts. After your counting, you concluded that “Apple”, “iPhone”, “iPod”, “iPad” are the most high-frequency words in his blog. Once one of these words were read by your smart script, it will
make a comment “MAI MAI MAI!”, and go on reading the post.
In order to make it more funny, you, as a fan of Sony, also want to make some comments about Sony. So you want to add a new rule to the script: make a comment “SONY DAFA IS GOOD!” when “Sony” appears.
[align=left]Input[/align]
A blog article described above, which contains only printable characters(whose ASCII code is between 32 and 127), CR(ASCII code 13, ‘\r’ in C/C++), LF(ASCII code 10, ‘\n’ in C/C++), please process input until EOF. Note all characters
are case sensitive.
The size of the article does not exceed 8KB.
[align=left]Output[/align]
Output should contains comments generated by your script, one per line.
[align=left]Sample Input[/align]
Apple bananaiPad lemon ApplepiSony
233
Tim cook is doubi from Apple
iPhoneipad
iPhone30 is so biiiiiiig Microsoft
makes good App.
[align=left]Sample Output[/align]
MAI MAI MAI!
MAI MAI MAI!
MAI MAI MAI!
SONY DAFA IS GOOD!
MAI MAI MAI!
MAI MAI MAI!
MAI MAI MAI!
思路:字符串匹配,使用 substr;
s.substr() :返回整条字符串。
s.substr(i, len):返回从索引i 截取长度len 的子串。
CODE:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
const int inf=0xfffffff;
typedef long long ll;
using namespace std;
string s;
int main()
{
//freopen("in", "r", stdin);
string a="Apple",b="iPhone", c="iPod", d="iPad",e="Sony";
while(getline(cin,s)){
//cout <<s<<endl;
int len = s.length();
for(int i = 0 ; i < s.size(); i ++){
if(s.substr(i, 5) == a.substr() ){
printf("MAI MAI MAI!\n");
i += 4;
}
if(s.substr(i, 6) == b.substr() ){
printf("MAI MAI MAI!\n");
i += 5;
}
if(s.substr(i,4) == c.substr() || s.substr(i,4) == d.substr()){
printf("MAI MAI MAI!\n");
i += 3;
}
if(s.substr(i,4) == e.substr()){
printf("SONY DAFA IS GOOD!\n");
i += 3;
}
}
//s.clear();
}
return 0;
}
DT is a big fan of digital products. He writes posts about technological products almost everyday in his blog.
But there is such few comments of his posts that he feels depressed all the day. As his best friend and an excellent programmer, DT asked you to help make his blog look more popular. He is so warm that you have no idea how to refuse. But you are unwilling to
read all of his boring posts word by word. So you decided to write a script to comment below his posts automatically.
After observation, you found words “Apple” appear everywhere in his posts. After your counting, you concluded that “Apple”, “iPhone”, “iPod”, “iPad” are the most high-frequency words in his blog. Once one of these words were read by your smart script, it will
make a comment “MAI MAI MAI!”, and go on reading the post.
In order to make it more funny, you, as a fan of Sony, also want to make some comments about Sony. So you want to add a new rule to the script: make a comment “SONY DAFA IS GOOD!” when “Sony” appears.
[align=left]Input[/align]
A blog article described above, which contains only printable characters(whose ASCII code is between 32 and 127), CR(ASCII code 13, ‘\r’ in C/C++), LF(ASCII code 10, ‘\n’ in C/C++), please process input until EOF. Note all characters
are case sensitive.
The size of the article does not exceed 8KB.
[align=left]Output[/align]
Output should contains comments generated by your script, one per line.
[align=left]Sample Input[/align]
Apple bananaiPad lemon ApplepiSony
233
Tim cook is doubi from Apple
iPhoneipad
iPhone30 is so biiiiiiig Microsoft
makes good App.
[align=left]Sample Output[/align]
MAI MAI MAI!
MAI MAI MAI!
MAI MAI MAI!
SONY DAFA IS GOOD!
MAI MAI MAI!
MAI MAI MAI!
MAI MAI MAI!
思路:字符串匹配,使用 substr;
s.substr() :返回整条字符串。
s.substr(i, len):返回从索引i 截取长度len 的子串。
CODE:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
const int inf=0xfffffff;
typedef long long ll;
using namespace std;
string s;
int main()
{
//freopen("in", "r", stdin);
string a="Apple",b="iPhone", c="iPod", d="iPad",e="Sony";
while(getline(cin,s)){
//cout <<s<<endl;
int len = s.length();
for(int i = 0 ; i < s.size(); i ++){
if(s.substr(i, 5) == a.substr() ){
printf("MAI MAI MAI!\n");
i += 4;
}
if(s.substr(i, 6) == b.substr() ){
printf("MAI MAI MAI!\n");
i += 5;
}
if(s.substr(i,4) == c.substr() || s.substr(i,4) == d.substr()){
printf("MAI MAI MAI!\n");
i += 3;
}
if(s.substr(i,4) == e.substr()){
printf("SONY DAFA IS GOOD!\n");
i += 3;
}
}
//s.clear();
}
return 0;
}
相关文章推荐
- HDU 5007 Post Robot(字符串寻找)
- 字符串水题 hdu 5007 Post Robot
- HDU - 5007 Post Robot
- hdu 5007(字符串水题)
- HDU 5007 Post Robot(西安网络赛A题)
- hdu 5007 Post Robot
- hdu 5007 Post Robot
- 【HDU 5007】Post Robot
- hdu 5007 Post Robot
- HDU 5007 Post Robot
- hdu 5007 Post Robot
- hdu 5007 水 字符串
- HDU5007——Post Robot(2014 年 西安区域赛)
- HDU 1036 字符串模拟题
- hdu 4039 字符串模拟题
- hdu 2017 字符串统计 解题报告
- hdu 3788 字符串
- hdu_2017_字符串统计
- [Hdu 2222] 字符串(三) {Aho-Corasick自动机}
- hdu 4054 字符串处理