J - Ananagrams(map+vector)
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
解题思路:题目的意思就是找出唯一的(不与其他字符串串内重排后相同的)字符串,将它们排序并输出。做法:用map记录所有字符串内部字符(所有大写改成小写。为什么呢?上面的红色部分就已经解释了,我们需要统一换成大写或者小写)重排后的字符串的个数,用两个vector,一个用来保存文本中的所有单词,另一个保存出现次数为1的单词,最后排序输出即可。
AC代码:
#include<bits/stdtr1c++.h>
using namespace std;
string repeat(string tmp){
for(size_t i=;i<tmp.size();++i)
if(isupper(tmp[i]))tmp[i]+=;//如果是大写字母,全部改为小写字母
sort(tmp.begin(),tmp.end());//串内重新排序
return tmp;
}
int main(){
string str,tp;
vector<string> vec,vrc;
map<string,int> mp;//记录串内字符重排后出现的次数
while(cin>>str&&str!="#"){
vec.push_back(str);
tp=repeat(str);
if(!mp.count(tp))mp[tp]=;//如果还没有出现,次数先初始化为0
mp[tp]++;//统计相同字符串出现的次数
}
vrc.clear();//清空容器
for(size_t i=;i<vec.size();++i)
if(mp[repeat(vec[i])]==)vrc.push_back(vec[i]);//如果该单词出现的次数为1,则满足条件,保存在vrc容器中
sort(vrc.begin(),vrc.end());//排序
for(size_t i=;i<vrc.size();++i)
cout<<vrc[i]<<endl;
return ;
}
J - Ananagrams(map+vector)的更多相关文章
- 2018.09.26 洛谷P2464 [SDOI2008]郁闷的小J(map+vector)
传送门 本来出题人出出来想考数据结构的. 但是我们拥有map+vector/set这样优秀的STL,因此直接用map离散化,vector存下标在里面二分找答案就行了. 代码: #include< ...
- 【PTA 天梯赛训练】词频统计(map+vector)
请编写程序,对一段英文文本,统计其中所有不同单词的个数,以及词频最大的前10%的单词. 所谓“单词”,是指由不超过80个单词字符组成的连续字符串,但长度超过15的单词将只截取保留前15个单词字符.而合 ...
- Educational Codeforces Round 11——A. Co-prime Array(map+vector)
A. Co-prime Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Sicily 1299 Academy Awards (map + vector)集装箱
链接:http://soj.me/show_problem.php?pid=1299&cid= Description Selected from 3,850 teams from 1,329 ...
- 斗地主的综合案例实现(Map有序)
斗地主的综合案例实现(Map有序) 整体思路 代码实现 import java.util.ArrayList; import java.util.Collections; import java.ut ...
- 分布式基础学习(2)分布式计算系统(Map/Reduce)
二. 分布式计算(Map/Reduce) 分 布式式计算,同样是一个宽泛的概念,在这里,它狭义的指代,按Google Map/Reduce框架所设计的分布式框架.在Hadoop中,分布式文件 系统,很 ...
- 分布式基础学习【二】 —— 分布式计算系统(Map/Reduce)
二. 分布式计算(Map/Reduce) 分布式式计算,同样是一个宽泛的概念,在这里,它狭义的指代,按Google Map/Reduce框架所设计的分布式框架.在Hadoop中,分布式文件系统,很大程 ...
- IntelliJ IDEA中,mybatis的配置文件(map.xml)无法编译到class文件夹下
编译工具:IntelliJ IDEA 项目结构:maven 项目框架:SSM 问题:java目录下,mybatis的配置文件(map.xml)无法编译到class文件夹下 问题原因:在idea中,直接 ...
- STL——容器(Map & multimap)的插入与迭代器
1. 容器(Map & multimap)的插入 map.insert(...); //往容器插入元素,返回pair<iterator,bool> map中插入元素的四种方式 ...
随机推荐
- Caocao's Bridges-HDU4738(Tarjin+求桥)
http://acm.hdu.edu.cn/showproblem.php?pid=4738 题目大意: 给定n个点和m条边 和每条边的价值,求桥的最小价值(最小桥) 看着挺简单的但是有好多细节: ...
- Eclipse 远程tomcat调试程序
Eclipse 远程tomcat调试程序 很多时候我们把代码部署到云服务器上,需要调试的时候可以选择远程调试,既节省时间,效率又高.下面详细介绍如何进行远程调试. 1.1. 创建startup-deb ...
- MYSQL 时间数据类型
- 在GNS3下使用Cisco SDM 的教程
安装步骤: 1..先安装jre-6u17-windows-i586se (最新版的)如图: 点击安装,直到安装完成. © 2.安装SDM2.5中文版SDM-V25 如图 : 出现欢迎安装向导,点击下一 ...
- AutoCAD如何设置线宽
一般要求粗实线粗实线0.4,细实线0.2mm. 1 先打开图层特性管理器,新建一个图层,专门放粗实线(我起名叫"我的粗实线",颜色设置为紫色,线宽为0.4mm),此前的乱七八糟的图 ...
- 华为OJ2011-最长公共子串
一.题目描述 描述: 计算两个字符串的最大公共子串(Longest Common Substring)的长度,字符区分大小写. 输入: 输入两个字符串 输出: 输出一个整数 样例输入: asdfas ...
- MariaDB ----增删改
1> 查看表的(创建)状态: show careate table students1; 注: 自增, (保护机制) 好马不吃回头草 2>查看 字段:( 查看表的结构) des ...
- 2015/12/30 Java语法学习
①标识符包括:包名.类名.方法名.变量名.常量名.属性名 标识符书写规则:1,标识符由字母.数字._.$ 组成 2,数字不能出现在开始位置 ...
- Chrome查看JavaScript函数
在页面上右键view page source(Ctrl+U),然后在弹出来的界面可以查找JavaScript函数 注意:这个只能看到内嵌在网页上的JavaScript函数 一般来讲,JavaScrip ...
- CSU 1806 Toll 自适应simpson积分+最短路
分析:根据这个题学了一发自适应simpson积分(原来积分还可以这么求),然后就是套模板了 学习自适应simpson积分:http://blog.csdn.net/greatwall1995/arti ...