华为机试——在给定字符串中找出单词
2015-09-29 11:20
393 查看
华为机试——在给定字符串中找出单词
已知“单词”由大写字母和小写字母字符构成,其他非字母字符视为单词的间隔,如空格、问号、数字等等;另外单个字母不算单词;找到单词后,按照单词长度进行降序排序,(排序时如果长度相同,则按出现的顺序进行排序),然后输出到一个新的字符串中;如果某个单词重复出现多次,则只输出一次;如果整个输入的单词串中没有找到单词,请输出空串。输出的单词之间使用一个“空格”隔开,最后一个单一不加空格。
#include<iostream> #include<string> #include<ctype.h> #include<vector> #include<algorithm> using namespace std; bool cmp(string s1,string s2) { return s1.size()>s2.size(); } int main() { string str,word,strout; vector<string> wordlist,wordout; int count=0; string::size_type i=0; getline(cin,str); for(;i<str.size();i++) { while(isalpha(str[i])) { word.push_back(str[i]); count++; i++; } if(count>1) { wordlist.push_back(word); word.clear(); count=0; } else { word.clear(); count=0; } } stable_sort(wordlist.begin(),wordlist.end(),cmp); for(vector<string>::iterator it=wordlist.begin(); it!=wordlist.end();it++) { cout<<*it<<endl; int flag=0; for(vector<string>::iterator iter=wordout.begin(); iter!=wordout.end();iter++) if(!(*iter).compare(*it)) { flag=1; break; } if(!flag) { wordout.push_back(*it); } } if(wordout.size()>0) { cout<<*wordout.begin(); for(vector<string>::iterator it=wordout.begin()+1; it!=wordout.end();++it) { cout<<" "<<*it; } } else cout<<" "; return 0; }
相关文章推荐
- 推送处理
- 本地事务使用注意事项
- 推送处理
- 好词佳句
- linux查看占用内存/cpu最高的进程情况
- PAT(甲级)1101
- 图形验证码one
- UIWebView 的使用
- MySQL常用命令总结
- 关于函数形参的数组真实面目
- unrecognized command line option “-std=null” 及升级gcc的方式
- spring使用手动装载依赖的bean
- Meta标签中的format-detection属性及含义
- PAT(甲级)1100
- [前端] 定位光标位置
- springmvc和mybatis整合关键配置
- 黑马程序员学习(十七) java反射机制个人学习经验
- Android 回调实例详解
- Egit 解决冲突
- (转)android 消息推送.视频