首页 > 代码库 > HDOJ 5007 Post Robot--2014网络赛西安赛区A题

HDOJ 5007 Post Robot--2014网络赛西安赛区A题

题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=5007



Post Robot

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 193    Accepted Submission(s): 164


Problem Description
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.
 

Input
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.
 

Output
Output should contains comments generated by your script, one per line.
 

Sample Input
Apple bananaiPad lemon ApplepiSony 233 Tim cook is doubi from Apple iPhoneipad iPhone30 is so biiiiiiig Microsoft makes good App.
 

Sample Output
MAI MAI MAI! MAI MAI MAI! MAI MAI MAI! SONY DAFA IS GOOD! MAI MAI MAI! MAI MAI MAI! MAI MAI MAI!
 

Source
2014 ACM/ICPC Asia Regional Xi‘an Online
 
题意: 买买买~~ 索尼大法好!  输入一段字符串,每当出现“Apple”, “iPhone”, “iPod”, “iPad”  中的一个词则输出一句 MAI MAI MAI!  每出现一个Sony 则输出一句 SONY DAFA IS GOOD!

题解:简单字符串处理题~~ 拼的是速度&&细心   我的代码不大简洁的样子,待会看看其他人的如何。

AC代码:
#include<iostream>
#include<string>
#include<algorithm>
#define Max 10000
using namespace std; 
string str,map[]={"Apple","iPhone","iPod","iPad"};
string out[]={"MAI MAI MAI!","SONY DAFA IS GOOD!"};
int outa[Max],outb[Max];
int main(){  
	while(cin>>str){
		int len=str.size(),posa=0,posb=0,temp;
		for(int k=0;k<4;k++)
		for(int i=0;i<len;i++){
		if((temp=str.find(map[k],i))!=string::npos){
				outa[posa++]=temp;i=temp+1;	
		}
		}	
		for(int i=0;i<len;i++){
			if((temp=str.find("Sony",i))!=string::npos){
				outb[posb++]=temp;i=temp+1;	
			}
		}
	int i=0,j=0;
	sort(outa,outa+posa);
	sort(outb,outb+posb);
	while(i+j<posa+posb){
		if(i==posa) {
			cout<<out[1]<<endl; j++;
		}
		else if(j==posb){
			cout<<out[0]<<endl; i++;
		}
		else if(outa[i]<outb[j]){
			cout<<out[0]<<endl;i++;
		}
		else {
			cout<<out[1]<<endl;j++;
		}
	}		
}
	return 0;
}




HDOJ 5007 Post Robot--2014网络赛西安赛区A题