描述

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

样例输入

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
#

样例输出

Disk
NotE
derail
drIed
eye
ladder
soon

思路

用到两种数据结构map和set,map存入单词(在转换为小写且按照字母升序顺序后),set存入输入的单词

code

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<map>
#include<string>
#include<algorithm>
#include<set>
#include<sstream>
using namespace std; int main()
{
string input;
map<string, int> mp;
set<string> st;
while (getline(cin, input) && input != "#") {
istringstream is(input);
string word;
while (is >> word) {
string word1 = word;
transform(word1.begin(), word1.end(), word1.begin(), ::tolower);//将单词转换为小写
sort(word1.begin(), word1.end());
mp[word1]++;
st.insert(word);
}
}
for (set<string>::iterator iter = st.begin(); iter != st.end(); iter++) {
string temp = *iter;
transform(temp.begin(), temp.end(), temp.begin(), ::tolower);
sort(temp.begin(), temp.end());
if (mp[temp] == 1) {
cout << *iter << endl;
}
}
return 0;
}

Anagrams(字谜)的更多相关文章

  1. 049 Group Anagrams 字谜分组

    给定一个字符串数组,将相同字谜组合在一起.(字谜是指颠倒字母顺序而成的字)例如,给定 ["eat", "tea", "tan", " ...

  2. hadoop实战项目:查找相同字母组成的字谜

    前面我们学习了MapReduce编程思想和编程示例,那么本节课程同学们一起操练操练,动手完成下面的项目. 项目需求 一本英文书籍包含成千上万个单词或者短语,现在我们需要在大量的单词中,找出相同字母组成 ...

  3. 子串字谜substring anagrams

    [抄题]: 给定一个字符串 s 和一个 非空字符串 p ,找到在 s 中所有关于 p 的字谜的起始索引.字符串仅由小写英文字母组成,字符串 s 和 p 的长度不得大于 40,000.输出顺序无关紧要. ...

  4. C语言 · Anagrams问题

    问题描述 Anagrams指的是具有如下特性的两个单词:在这两个单词当中,每一个英文字母(不区分大小写)所出现的次数都是相同的.例如,"Unclear"和"Nuclear ...

  5. [LeetCode] Find All Anagrams in a String 找出字符串中所有的变位词

    Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...

  6. [LeetCode] Anagrams 错位词

    Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...

  7. Leetcode Anagrams

    Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...

  8. LeetCode Find All Anagrams in a String

    原题链接在这里:https://leetcode.com/problems/find-all-anagrams-in-a-string/ 题目: Given a string s and a non- ...

  9. Anagrams

    这题Leetcode上面的描述不清楚.怎么也得举两个例子吧,不然谁懂? 题目的意思是,给定一些字符串,比如["abc","cba","bac" ...

  10. LintCode Anagrams

    (记得import java.util.HashMap及Arrays, 首先字符串若为空或者数量为零, 则返回一个空的LinkedList) 1. 把string变为char数组, 再进行排序, 之后 ...

随机推荐

  1. 记录C++,base64解码写PDF文件遇到的坑

    不得不bb一下, 场景:用户传base64数据,我生成PDF文件保存到指定路径下 背景:在前人写好的工程上增加这个功能,工程中有base64的.h, .cpp 文件,我试了base64编码没有问题,所 ...

  2. 如何采用VuePress构建文档网站

    作者:倾城 博客: https://www.codingbrick.com 寄语:当你意识到面子不重要时,你才算个真正的成年人. 在建设博客的初期,我采用GitBook构建了编码专家的专栏系统.Git ...

  3. Spring-Cloud 组件之 Spring Cloud Eureka:服务注册与发现

    Spring Cloud Eureka:服务注册与发现 SpringCloud学习教程 SpringCloud Spring Cloud Eureka是Spring Cloud Netflix 子项目 ...

  4. 力扣571(MySQL)-给定数字的频率查询中位数(困难)

    题目: Numbers 表保存数字的值及其频率. 在此表中,数字为 0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 3,所以中位数是 (0 + 0) / 2 = 0. 请编写一个查询 ...

  5. 力扣177(MySQL)-第N高的薪水(中等)

    题目: 表: Employee 编写一个SQL查询来报告 Employee 表中第 n 高的工资.如果没有第 n 个最高工资,查询应该报告为 null . 查询结果格式如下所示 示例1: 示例2: 解 ...

  6. java应用提速(速度与激情)

    简介: 本文将阐述通过基础设施与工具的改进,实现从构建到启动全方面大幅提速的实践和理论. 作者 | 阿里巴巴CTO技术来源 | 阿里开发者公众号 联合作者:道延 微波 沈陵 梁希 大熊 断岭 北纬 未 ...

  7. Fluid给数据弹性一双隐形的翅膀 (1) -- 自定义弹性伸缩

    简介: 弹性伸缩作为Kubernetes的核心能力之一,但它一直是围绕这无状态的应用负载展开.而Fluid提供了分布式缓存的弹性伸缩能力,可以灵活扩充和收缩数据缓存. 它基于Runtime提供了缓存空 ...

  8. CDP 平台简介

    ​简介: EDC 建立在 Cloudera Data Platform(CDP) 之上,该产品结合了 Cloudera Enterprise Data Hub 和 Hortonworks Data P ...

  9. [Contract] Solidity 多种访问控制 (Access Control) 实现方式

    在 solidity 中控制访问,一般是通过 modifier 修饰符方法来直接做. 那么对于稍复杂的多种访问控制,通常需要一个统一操作的模块化类库. 现在已经有了这样的类库存在,我们通过一个实现功能 ...

  10. 2019-4-29-win10-uwp-使用-Border-布局

    title author date CreateTime categories win10 uwp 使用 Border 布局 lindexi 2019-04-29 12:29:45 +0800 201 ...