描述

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. 【FAQ】用户访问次数不变,访问时长却突增2倍,分析服务发生数据异常该如何解决?

    在产品运营的工作过程中,需要每日关注产品的核心指标变化情况,监控其整体运营状况.华为分析服务提供查看吸引新用户卡片,该卡片展示了新增用户数.人均会话次数.人均访问时长.人均页面访问数.借助该页面运营可 ...

  2. openGauss/MogDB数据库安装部署之xlog目录设置

    openGauss/MogDB 数据库安装部署之 xlog 目录设置 本文出处:https://www.modb.pro/db/176915 关于 xlog xlog 文件是一个记录事务日志的文件,它 ...

  3. 重新整理.net core 计1400篇[四] (.net core 修改sdk )

    前言 可能有些人还不知道什么是sdk,software development kit,中文是软件开发包的意思. 然后什么是软件开发包? 软件开发工具包是一些被软件工程师用于为特定的软件包.软件框架. ...

  4. 6个高级Vue3知识技巧

    Vue 3是一个非常流行的前端框架,广泛应用于大型互联网企业和个人项目. 虽然我们已经熟悉了一些常见的 Vue 3 知识,但还有一些不太常见但实用性很强的点可以帮助我们进一步优化和提升 Vue 3 应 ...

  5. 第九課-Channel Study For Caller Client Api

    1.浏览Mirth Connect开放Client API 2.启动Vs2019,编程测试Client Api的调用 开启你在宇宙第一IDE下熟悉的.net之旅吧!

  6. 力扣34(java)-在排序数组中查找元素的第一个和最后一个位置(中等)

    题目: 给你一个按照非递减顺序排列的整数数组 nums,和一个目标值 target.请你找出给定目标值在数组中的开始位置和结束位置. 如果数组中不存在目标值 target,返回 [-1, -1]. 你 ...

  7. App隐私合规“免费”自动化检测

    简介: App隐私合规检测提供了全面的隐私合规检测报告和专家建议,从确保形式合规(隐私政策文本合规性)及实质合规(代码层合规性)的一致性,从个人信息收集.权限使用场景.超范围采集.隐私政策.三方SDK ...

  8. EasyNLP带你玩转CLIP图文检索

    简介: 本文简要介绍CLIP的技术解读,以及如何在EasyNLP框架中玩转CLIP模型. 作者:熊兮.章捷.岑鸣.临在 导读 随着自媒体的不断发展,多种模态数据例如图像.文本.语音.视频等不断增长,创 ...

  9. Serverless Kubernetes:理想,现实与未来

    简介: 当前 Serverless 容器的行业趋势如何?有哪些应用价值?如果 Kubernetes 天生长在云上,它的架构应该如何设计?Serverless 容器需要哪些基础设施?阿里云容器服务产品负 ...

  10. MaxCompute Tunnel 技术原理及开发实战

    简介: MaxCompute(原名ODPS)是一种快速.完全托管的EB级数据仓库解决方案, 致力于批量结构化数据的存储和计算,为用户提供数据仓库的解决方案及分析建模服务.Tunnel是MaxCompu ...