UVa156.Ananagrams
题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=92
13913904 | 156 | Ananagrams | Accepted | C++ | 0.009 | 2014-07-20 15:31:41 |
Ananagrams |
Most crossword puzzle fans are used to anagrams--groups of words with the same letters in different orders--for example OPTS, SPOT, STOP, POTS and POST. Some words however do not have this attribute, no matter how you rearrange their letters, you cannot form another word. Such words are called ananagrams, an example is QUIZ.
Obviously such definitions depend on the domain within which we are working; you might think that ATHENE is an ananagram, whereas any chemist would quickly produce ETHANE. One possible domain would be the entire English language, but this could lead to some problems. One could restrict the domain to, say, Music, in which case SCALE becomes a relative ananagram (LACES is not in the same domain) but NOTE is not since it can produce TONE.
Write a program that will read in the dictionary of a restricted domain and determine the relative ananagrams. Note that single letter words are, ipso facto, relative ananagrams since they cannot be ``rearranged'' at all. The dictionary will contain no more than 1000 words.
Input
Input will consist of a series of lines. No line will be more than 80 characters long, but may contain any number of words. Words consist of up to 20 upper and/or lower case letters, and will not be broken across lines. Spaces may appear freely around words, and at least one space separates multiple words on the same line. Note that words that contain the same letters but of differing case are considered to be anagrams of each other, thus tIeD and EdiT are anagrams. The file will be terminated by a line consisting of a single #.
Output
Output will consist of a series of lines. Each line will consist of a single word that is a relative ananagram in the input dictionary. Words must be output in lexicographic (case-sensitive) order. There will always be at least one relative ananagram.
Sample input
- ladder came tape soon leader acme RIDE lone Dreis peat
- ScAlE orb eye Rides dealer NotE derail LaCeS drIed
- noel dire Disk mace Rob dries
- #
Sample output
- Disk
- NotE
- derail
- drIed
- eye
- ladder
- soon
- 解题思路:运用map关联数组,就仿佛开挂一样。简单的操作与排序即可,略水。
- #include <iostream>
- #include <cstring>
- #include <cstdlib>
- #include <cctype>
- #include <cstdio>
- #include <algorithm>
- #include <numeric>
- #include <cmath>
- #include <map>
- #include <vector>
- using namespace std;
- map<string, int> cnt;
- vector<string> words;
- string cal_index(const string& s) {
- string ans = s;
- for (int i = ; i < ans.size(); ++ i) {
- ans[i] = tolower(ans[i]);
- }
- sort(ans.begin(), ans.end());
- return ans;
- }
- int main () {
- string str;
- while (cin >> str) {
- if (str == "#") {
- break;
- }
- words.push_back(str);
- string sort_str = cal_index(str);
- if (!cnt.count(sort_str)) {
- cnt[sort_str] = ;
- }
- cnt[sort_str] ++;
- }
- vector<string> ans;
- for (int i = ; i < words.size(); ++ i) {
- if (cnt[cal_index(words[i])] == ) {
- ans.push_back(words[i]);
- }
- }
- sort(ans.begin(), ans.end());
- for (int i = ; i < ans.size(); ++ i) {
- cout << ans[i] << endl;
- }
- return ;
- }
UVa156.Ananagrams的更多相关文章
- UVa156 Ananagrams(映射map)
Ananagrams 题目 Most crossword puzzle fans are used to anagrams--groups of words with the same letters ...
- UVa-156 Ananagrams(map映射)
#include <iostream> #include <algorithm> #include <cmath> #include <cstdio> ...
- UVa-156 Ananagrams 反片语【map】【vector】
题目链接:https://vjudge.net/contest/211547#problem/D 题目大意: 输入一些单词,找出所有满足以下条件的单词:该单词不能通过字母重排,得到输入文本中的另外一些 ...
- uva-156(Ananagrams UVA - 156)
map容器的模板题,判断是否能交换字母顺序变成另外一个单词,只需要先把单词都变成小写字母.然后再按字母字典序排序,放入map中进行计数,然后把计数为一的再放入另一个容器,再排序输出即可 我的代码(刘汝 ...
- 【UVA - 156 】Ananagrams (set,map,vector)
Ananagrams Descriptions: Most crossword puzzle fans are used to anagrams--groups of words with the ...
- ACM题目————STL练习之Ananagrams
Description Most crossword puzzle fans are used to anagrams--groups of words with the same letters i ...
- STL语法——映射:map 反片语(Ananagrams,UVa 156)
Description Most crossword puzzle fans are used to anagrams--groups of words with the same letters i ...
- uva 156 - Ananagrams (反片语)
csdn:https://blog.csdn.net/su_cicada/article/details/86710107 例题5-4 反片语(Ananagrams,Uva 156) 输入一些单词,找 ...
- Winter-2-STL-F Ananagrams 解题报告及测试数据
Time Limit:3000MS Memory Limit:0KB Description Most crossword puzzle fans are used to anagrams- ...
随机推荐
- python字符串(移除空白,长度,索引,分割,切片,拼接,格式化输出)
常用功能: 移除空白: >>> name = "meng" >>> name 'meng' >>> name.strip() ...
- Python字典的操作与使用
字典的描述 字典是一种key-value的数据类型,使用就像我们上学用的字典,通过拼音(key)来查对应字的详细内容(value). 字典的特性 1.字典是无序的(不像列表一样有下标,它通过key来获 ...
- Android中获取网页表单中的数据实现思路及代码
在Android中获取网页里表单中的数据具体实现代码如下,感兴趣的各位可以参考过下哈,希望对大家有所帮助 MainActivity如下: 复制代码 代码如下: package cn.testjavas ...
- 一起talk C栗子吧(第二十回:C语言实例--括号匹配)
各位看官们,大家好.前几回中咱们说了堆栈的原理,而且举了实际的样例进行讲解,这一回咱们说的例 子是:括号匹配. 括号匹配使用了堆栈的原理,大家能够从样例看出来.所以我们把它们放在一起.闲话 休提.言归 ...
- Android高级图片滚动控件,编写3D版的图片轮播器
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/17482089 大家好,好久不见了,最近由于工作特别繁忙,已经有一个多月的时间没写博 ...
- 慕课linux学习笔记(一)centOS的安装
在VMware8上安装centos6.3 准备的文件 新建虚拟机 选择新建一个空的虚拟机 选择linux和centos 分配20G的硬盘空间 ' 修改配置 调整内存空间 桥接:虚拟机和真实机通讯使用的 ...
- docke 网络配置2
一,docker 的bridge模式是和vmware中的nat模式类似的,但是如果想要弄成和vmwae中的bridge怎么办呢? 说明,bridge模式获取的Ip是与宿主机的ip是出于同一个网段的. ...
- 使用堆栈结构进行字符串表达式("7*2-5*3-3+6/3")的计算
问题: 给定字符串String str = "7*2-5*3-3+6/3", 求出字符串里面表达式的结果? 像javascript有自带的eval()方法,可以直接计算.但java ...
- Action重定向总结
[HttpPost] public ActionResult StudentList( string StudName, string studName, DateTime BirthDay, For ...
- Activity-在ListFragment中为ListView增加空白视图
有两种方法可以实现为ListView添加空白视图.但是原理都一样: 第一种方法是XML+代码添加: 1.定义emptyView视图 2.调用AdapterView的setEmptyView(empt ...