Anagrams(字谜)
描述
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(字谜)的更多相关文章
- 049 Group Anagrams 字谜分组
给定一个字符串数组,将相同字谜组合在一起.(字谜是指颠倒字母顺序而成的字)例如,给定 ["eat", "tea", "tan", " ...
- hadoop实战项目:查找相同字母组成的字谜
前面我们学习了MapReduce编程思想和编程示例,那么本节课程同学们一起操练操练,动手完成下面的项目. 项目需求 一本英文书籍包含成千上万个单词或者短语,现在我们需要在大量的单词中,找出相同字母组成 ...
- 子串字谜substring anagrams
[抄题]: 给定一个字符串 s 和一个 非空字符串 p ,找到在 s 中所有关于 p 的字谜的起始索引.字符串仅由小写英文字母组成,字符串 s 和 p 的长度不得大于 40,000.输出顺序无关紧要. ...
- C语言 · Anagrams问题
问题描述 Anagrams指的是具有如下特性的两个单词:在这两个单词当中,每一个英文字母(不区分大小写)所出现的次数都是相同的.例如,"Unclear"和"Nuclear ...
- [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 ...
- [LeetCode] Anagrams 错位词
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- Leetcode Anagrams
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- LeetCode Find All Anagrams in a String
原题链接在这里:https://leetcode.com/problems/find-all-anagrams-in-a-string/ 题目: Given a string s and a non- ...
- Anagrams
这题Leetcode上面的描述不清楚.怎么也得举两个例子吧,不然谁懂? 题目的意思是,给定一些字符串,比如["abc","cba","bac" ...
- LintCode Anagrams
(记得import java.util.HashMap及Arrays, 首先字符串若为空或者数量为零, 则返回一个空的LinkedList) 1. 把string变为char数组, 再进行排序, 之后 ...
随机推荐
- 记录C++,读文件返回base64数据
读文件返回base64函数: void CZZUser::hidFileToBase(const char* filePath) { // 文件 转 base64 // 计算文件长度 unsigned ...
- ArkUI框架,更懂程序员的UI信息语法
原文:https://mp.weixin.qq.com/s/LQA6AYiG8O_AeGE1PZwxZg,点击链接查看更多技术内容. ArkUI框架简化代码的"秘密" 在传统 ...
- 如何采用VuePress构建文档网站
作者:倾城 博客: https://www.codingbrick.com 寄语:当你意识到面子不重要时,你才算个真正的成年人. 在建设博客的初期,我采用GitBook构建了编码专家的专栏系统.Git ...
- Cache Aside Pattern缓存+数据库读写模式的分析
1.Cache Aside Pattern(1)读的时候,先读缓存,缓存没有的话,那么就读数据库,然后取出数据后放入缓存,同时返回响应 (2)更新的时候,先删除缓存,然后再更新数据库 2.为什么是删除 ...
- 第一次blog
前言:我在大一上学期学习了c语言,然后在下学期学习了第二门语言java,因为之前c语言学的挺一般的,然后在这学期学习java感觉还是挺不简单的,要自学很多东西,在这段时间里,我学习了JAVA的基本语法 ...
- 剑指企业级云原生,阿里云 CNFS 如何破局容器持久化存储困境
简介: 云原生趋势下,应用容器化比例正在快速增长,Kubernetes 也已成为云原生时代新的基础设施. 据 Forrester 预测,到 2022 年, 全球企业及组织在生产环境运行容器化应用.观察 ...
- C# 采集知网
采集知网 WebClient /// <summary> /// 支持 Session 和 Cookie 的 WebClient. /// </summary> public ...
- 实验8 #第8章 Verilog有限状态机设计-2 #Verilog #Quartus #modelsim
2. 汽车尾灯控制器 2.1 实验要求:设计一个汽车尾灯控制电路. (1)功能:汽车左右两侧各有3个尾灯,要求控制尾灯按如下规则亮灭. 汽车沿直线行驶时,两侧指示灯全灭. 右转弯时,左侧的指示灯全灭, ...
- Java 泛型,这次面试我表现很棒!
public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryBy ...
- CF746 期望+逆序对
Link 题意:给定一个 \(1\) 到 \(n\) 的排列,等概率选一段区间 \([l, r]\) 随机排序,求期望逆序对数. \[E = \dfrac{\sum(cnt_{[1, n]} - cn ...