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数组, 再进行排序, 之后 ...
随机推荐
- 【FAQ】用户访问次数不变,访问时长却突增2倍,分析服务发生数据异常该如何解决?
在产品运营的工作过程中,需要每日关注产品的核心指标变化情况,监控其整体运营状况.华为分析服务提供查看吸引新用户卡片,该卡片展示了新增用户数.人均会话次数.人均访问时长.人均页面访问数.借助该页面运营可 ...
- 如何使用 Grafana 监控文件系统状态
当 JuiceFS 文件系统部署完成并投入生产环境,接下来就需要着手解决一个非常重要的问题 -- 如何实时监控它的运行状态?毕竟,它可能正在为关键的业务应用或容器工作负载提供持久化存储支持,任何小小的 ...
- Hive 查看,删除分区
查看所有分区 show partitions 表名; 删除一般会有两种方案 1.直接删除hdfs文件 亲测删除hdfs路径后 查看分区还是能看到此分区 可能会引起其他问题 此方法不建议 2. 使用删除 ...
- 重新点亮linux 命令树————文本查看vi和vim[五]
前言 简单整理一下vi和vim,主要介绍一下四种模式. 正文 四种模式分别是: 正常模式(normal-mode) 插入模式(insert-mode) 命令模式(command-mode) 可视模式( ...
- EVA: Visual Representation Fantasies from BAAI
本文做个简单总结,博主不是做自监督领域的,如果错误,欢迎指正. 链接 Code: Official:baaivision/EVA MMpretrain:open-mmlab/mmpretrain/ ...
- .NET8中的Microsoft.Extensions.Http.Resilience库
接上一篇,https://www.cnblogs.com/vipwan/p/18129361 借助Aspire中新增的Microsoft.Extensions.ServiceDiscovery库,我们 ...
- Django框架——路由分发、名称空间、虚拟环境、视图层三板斧、JsonResponse对象、request获取文件、FBV与CBV、CBV源码剖析、模版层
路由分发 # Django支持每个应用都可以有自己独立的路由层.静态文件.模版层.基于该特性多人开发项目就可以完全解耦合,之后利用路由分发还可以整合到一起 多个应用都有很多路由与视图函数的对应关系 这 ...
- 企业版Spark Databricks + 企业版Kafka Confluent 联合高效挖掘数据价值
简介:本文介绍了如何使用阿里云的Confluent Cloud和Databricks构建数据流和LakeHouse,并介绍了如何使用Databricks提供的能力来挖掘数据价值,使用Spark ML ...
- 网不好怎么办?TLS握手带宽直降80%,BabaSSL是怎么做到的?| 龙蜥技术
简介:为了保障数据的安全性,客户端会先和服务器进行 TLS 握手,有什么办法可以减少 TLS 握手的带宽消耗呢? 编者按:BabaSSL 是一款开源的密码库产品,在 GitHub 和龙蜥社区开源,并 ...
- dotnet 启动进程传入不存在的文件夹作为工作目录行为变更
本文记录在 dotnet 下,启动进程,传入不存在的文件夹作为进程的工作目录,分别在 .NET Framework 和 .NET Core 的行为 在 dotnet 6 下,可以使用 ProcessS ...