算法竞赛入门经典 第二版 习题5-5 复合词 Compound Words uva10391
来源:互联网 发布:淘宝中国制造怎么申请 编辑:程序博客网 时间:2024/06/05 02:01
题目:https://vjudge.net/problem/UVA-10391
思路:用vector存下单词,因为单词已按字典序排列于是复合词前半个单词一定在这个复合词之前,于是遍历寻找在首字母更变之前是否有单词的前半部分和当前单词相同,然后再在整个vector中寻找是否有后半个部分的单词,若有,即是答案,用set存下来。一是set的有序性可以让答案按字典序排列,二是可以去重。最后输出即可。
注:题干有个错——A two-word compound word is a
word in the dictionary that is the concatenation of exactly two other words in the dictionary.
这句话有毒,明明说了要用两不同单词合成结果加判断条件就Wrong Answer……
代码:
#include <iostream>#include <string>#include <cstdio>#include <iomanip>#include <map>#include <set>#include <vector>#include <cstring>#include <algorithm>using namespace std;set<string> ans;vector<string> data;bool issame(const string &part, const string &whole){ if(whole.size()<=part.size()) { return false; } for(int i=0; i<part.size(); i++) { if(part[i]!=whole[i]) { return false; } } return true;}int main(){ string s; while(cin >> s) { data.push_back(s); } for(int i=0; i<data.size()-1; i++) { if(data[i].empty()) { continue; } for(int j=i+1; j<data.size(); j++) { if(data[j].empty()) { continue; } if(data[i][0]!=data[j][0]) { break; } if(issame(data[i], data[j])) { string t; for(int k=data[i].size(); k<data[j].size(); k++) { t.push_back(data[j][k]); } vector<string>::iterator iter = find(data.begin(), data.end(), t); if(iter!=data.end()) { ans.insert(data[j]); } } } } for(set<string>::iterator it = ans.begin(); it!=ans.end(); ++it) { cout << *it << endl; } return 0;}
0 0
- 算法竞赛入门经典 第二版 习题5-5 复合词 Compound Words uva10391
- 算法竞赛入门经典 习题5-5 复合词 UVa10391
- 算法竞赛入门经典(第2版)习题5-5 复合词 UVa10391
- [刷题]算法竞赛入门经典(第2版) 5-5/UVa10391 - Compound Words
- UVA10391:Compound Words(复合词)
- 习题5-5 复合词 UVa10391
- 习题5-5 复合词(Compound Words, UVa 10391)
- 5-5 UVA 10391 Compound Words复合词
- Uva10391 Compound Words 【俩方法:1.二分;2.set集合】【习题5-5】
- 算法竞赛入门经典(第二版) 习题3-5 谜题(Puzzle) UVa227 Finals1993
- 《算法竞赛入门经典第二版》习题2-5分数化小数问题
- 《算法竞赛入门经典(第二版)》第三章 习题(1-5)
- 算法竞赛入门经典 第二版 习题4-5 IP网络 IP Networks uva1590
- 算法竞赛入门经典 第二版 习题5-1 代码对齐 Alignment of Code uva1593
- 算法竞赛入门经典 第二版 习题5-2 Ducci序列 Ducci Sequence uva1594
- 算法竞赛入门经典 第二版 习题5-3 卡片游戏 Throwing cards away I uva10935
- 算法竞赛入门经典 第二版 习题5-4 交换学生 Foreign Exchange uva10763
- 算法竞赛入门经典 第二版 习题5-6 对称轴 Symmetry uva1595
- Codeforces Round #301 (Div. 2)C. Ice Cave(广搜BFS)
- 我的第一篇博客
- Android深入--01--起源以及准备
- C语言模拟实现c++的一个date类
- 第一次C++上机项目3
- 算法竞赛入门经典 第二版 习题5-5 复合词 Compound Words uva10391
- android4.3 解压、打包boot.img新脚本
- 735C Tennis Championship
- EasyUI Tabs选项卡自适应浏览器宽度和高度
- 雅思专家告知大家如何正确看待美国大学最新排名
- Comparable与Comparator的区别
- ajax json 前台传数据到后台,后台接收并返回json 数据
- 并查集理解
- UnityEditor 打开对应类