Description

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

中文大意:

输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文本中的另外一个单词。

在判断是否满足条件时,字母不分大小写,但在输入时应保留输入中的大小写,按字典序进行排列(所有大写字母在小写字母的前面)

分析:

题意:

从若干个单词中找出只出现过一次的单词,前提是不区分大小写,并且字母的顺序也不一定,例如:abc
和cba 就是属于同一个单词(因为题目是说值出现一次的单词,所以代码中必须有一个计数的变量)

思路:

判断两个单词是否可以通过重排列得到,把两个单词排序,然后比较两个单词是否相同,若相同则可以通过重新排列得到,所以对每个输入的单词进行标准化,即把单词中的字母转化为小写字母

(判断单词重排时,不区分大小写)然后对该单词进行排序,然后用map来储存标准化后的单词

1.写一个标准化函数(实现大写字母转换为小写(tolower()函数),单词排序。注意使用const是为了不改变s的初值)
2.两个vector容器(words,ans),一个map容器(cnt)
words存储所有的单词
map存储标准化后对应单词以及出现次数的值,相当于一个表格。
words经过查表map,把对应的符合值给ans
3.输出

代码:

 # include <iostream>//输入输出流
 # include <string>
 # include <cctype> //包含 tolower 函数
 # include <vector>  //不定长数组
 # include <map>     //映射,在本例中36行有体现
 # include <algorithm> //一些函数,sort等
 using namespace std;

 map<string, int> cnt; //定义映射
 vector<string> words; //一个叫words的不定长数组

 //将单词s进行标准化
 string repr(const string& s)//声明变量s
 {
     string ans = s;//定义字符串类型数组,并使用第三方变量进行转换,并最终输出时还能保留大写部分
     ; i < ans.length(); i++)
         ans[i] = tolower(ans[i]);//全部转化成小写字母
     sort(ans.begin(), ans.end());//从小到大按字典序进行排序
     return ans;
 }

 int main()
 {
     ;
     string s;
     while (cin >> s)
     {
         ] == '#') break;//遇到‘#’结束循环
         words.push_back(s);//存入vector
         string r = repr(s);//给所有单词进行排列,上面13行用“引用”的原因,方便调用
         ; //!cnt.count(r) 的值,不是0就是1
         cnt[r]++;      //count 是返回容器中r出现的次数,统计重新组成新单词的个数
     }
     vector<string> ans;//又重新复制了一份,用另外一个存储
     ; i < words.size(); i++)
       )//此时用到了“映射”,如果不是映射的话,一个是字符串一个是int型,不能用等号的;找到唯一的单词
             ans.push_back(words[i]);
     sort(ans.begin(), ans.end());//排序
     ; i < ans.size(); i++)
         cout << ans[i] << "\n";

     getchar();
     getchar();

     ;
 }

革命尚未成功,同志们仍需努力......

 

STL语法——映射:map 反片语(Ananagrams,UVa 156)的更多相关文章

  1. 反片语 (Ananagrams,UVa 156)

    题目描述: #include <iostream> #include <string> #include <cctype> #include <vector& ...

  2. C++ STL之映射map的使⽤

    写在最前面:本文摘录于柳神笔记: map 是键值对,⽐如⼀个⼈名对应⼀个学号,就可以定义⼀个字符串 string 类型的⼈名为“键”,学 号 int 类型为“值”,如 map<string, i ...

  3. Ananagrams UVA - 156

      Most crossword puzzle fans are used to anagrams - groups of words with the same letters in differe ...

  4. uva-156(Ananagrams UVA - 156)

    map容器的模板题,判断是否能交换字母顺序变成另外一个单词,只需要先把单词都变成小写字母.然后再按字母字典序排序,放入map中进行计数,然后把计数为一的再放入另一个容器,再排序输出即可 我的代码(刘汝 ...

  5. uva 156 - Ananagrams (反片语)

    csdn:https://blog.csdn.net/su_cicada/article/details/86710107 例题5-4 反片语(Ananagrams,Uva 156) 输入一些单词,找 ...

  6. C++标准模板库(STL)之Map

    1.Map的常用用法 map:映射.可以将任何基本类型,结构体,STL容器映射到任何基本类型包括容器. 使用map,需要加map的头文件,#include<map>和using names ...

  7. STL中的map、unordered_map、hash_map

    转自https://blog.csdn.net/liumou111/article/details/49252645 在之前使用STL时,经常混淆的几个数据结构,特别是做Leetcode的题目时,对于 ...

  8. [知识点]C++中STL容器之map

    UPDATE(20190416):写完vector和set之后,发现不少内容全部引导到map上了……于是进行了一定的描述补充与更正. 零.STL目录 1.容器之map 2.容器之vector 3.容器 ...

  9. STL 中的map 与 hash_map的理解

    可以参考侯捷编著的<STL源码剖析> STL 中的map 与 hash_map的理解 1.STL的map底层是用红黑树存储的,查找时间复杂度是log(n)级别: 2.STL的hash_ma ...

随机推荐

  1. 简单的利用JS来判断页面是在手机端还是在PC端打开的方法

    在移动设备应用越来越广泛的今天,许多网站都开始做移动端的界面展示,两者屏幕尺寸差异很大,所以展示的内容也有所差别.于是就遇到一个问题,如何判断你的页面是在移动端还是在PC端打开的,很简单的问题,那我们 ...

  2. Go 并发随机打印1-n

    package main import (     "fmt"     "math/rand"     "sync"     "t ...

  3. Android -- 带你从源码角度领悟Dagger2入门到放弃(二)

    1,接着我们上一篇继续介绍,在上一篇我们介绍了简单的@Inject和@Component的结合使用,现在我们继续以老师和学生的例子,我们知道学生上课的时候都会有书籍来辅助听课,先来看看我们之前的Stu ...

  4. PHP 魔术方法__set() __get() 方法

    a); //output: 123 var_dump($s->b); //output: 123 var_dump($s->c); //output: null var_dump($s-& ...

  5. Vue 普通对象数据更新与 file 对象数据更新

    最近在做一个多图片上传的组件,需求是做到多文件依次上传,并显示上传进度条. 逻辑部分实现了以后,在更新进度条视图的时候出现一点问题:动态计算生产的进度 progress 属性不会自动更新. 原来的代码 ...

  6. jQuery选择器与CSS选择器

    1. 通过位置选择的几个操作: :first:默认情况下是相对整个页面来说的第一个,如:li:first表示整个页面的第一个li元素,而ul li:first表示整个页面的第一个li元素,并且是在ul ...

  7. Less与Sass

    less 1.变量    声明变量:@变量名:变量值 使用变量:@变量名 >>>Less中变量的类型 ①数字类:1 100px ②字符串:无引号字符串[red] 有引号字符串[&qu ...

  8. 我拖拖拖--H5拖放API基础篇

    不要搞错,本文不是讲如何拖地的.看过<javascript精粹>朋友应该知道,他实现拖放的过程比较复杂,现在时代不同了,我们用H5的新的拖放API就能非常方便的实现拖放效果了.最近在园子见 ...

  9. Unity 多屏(分屏)显示,Muti_Display

    Unity 多屏(分屏)显示,Muti_Display  最近项目有个需求,主要用于在展厅的展示游戏. 比如,在一个很大的展厅,很大的显示屏挂在墙上,我们不可能通过操作墙上那块显示器上的按钮来控制游戏 ...

  10. ui-router多视图+嵌套视图+传参综合练习

    ui-router多视图:页面上存在的多个ui-view,它们以名字区分: 嵌套视图:一个ui-view的一个状态下对应了一个html,这个html里面又有一个ui-view. 视图之间传参:用ui. ...