UVA 156 (13.08.04)
Ananagrams |
Most crossword puzzle fans are used to anagrams--groupsof words with the same letters in different orders--for exampleOPTS, SPOT, STOP, POTS and POST. Some words however do not have thisattribute, no matter how you rearrange their letters, you cannot formanother word. Such words are called ananagrams, an example isQUIZ.
Obviously such definitions depend on the domain within which we areworking; you might think that ATHENE is an ananagram, whereas anychemist would quickly produce ETHANE. One possible domain would be theentire English language, but this could lead to some problems. Onecould restrict the domain to, say, Music, in which case SCALE becomesa relative ananagram (LACES is not in the same domain) but NOTEis not since it can produce TONE.
Write a program that will read in the dictionary of a restricteddomain and determine the relative ananagrams. Note that single letterwords are, ipso facto, relative ananagrams since they cannot be``rearranged'' at all. The dictionary will contain no morethan 1000 words.
Input
Input will consist of a series of lines. No line will be more than 80characters long, but may contain any number of words. Words consist ofup to 20 upper and/or lower case letters, and will not be brokenacross lines. Spaces may appear freely around words, and at least onespace separates multiple words on the same line. Note that words thatcontain the same letters but of differing case are considered to beanagrams of each other, thus tIeD and EdiT are anagrams. The file willbe terminated by a line consisting of a single #.
Output
Output will consist of a series of lines. Each line will consist of asingle word that is a relative ananagram in the input dictionary.Words must be output in lexicographic (case-sensitive) order. Therewill 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
题意: 在一堆单词里, 找出那些没有字母调换顺序的单词
如acme, 在这堆词里还出现了came, 后者是因前者字母调换顺序得到, 故不用输出
而Disk就没有, 所以输出
另外注意: 不区分大小写;
做法:
首先, 我们可以想到, 只要两个单词可以通过调换字母顺序得到对方, 那么组成他们的字母是一样的, 这是第一步思路;
接着, 输出是要把单词按字典序输出, 故一开始输入完成后先qsort一下, 再用word数组, 把处理成小写后的单词储存下来;
然后, 我们把每个单词中的字母排成最小字典序;
最后,通过word字符数组进行搜索, 已排成字典序的单词若只出现一次, 就输出一开始未转化成小写的原始单词~
AC代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h> char str[125][25];
char word[125][25]; int cmp_str(const void *_a, const void *_b) {
char *a = (char*)_a;
char *b = (char*)_b;
return strcmp(a,b);
} int cmp_char(const void *_a, const void *_b) {
char *a = (char*)_a;
char *b = (char*)_b;
return *a - *b;
} int main() {
char tmp[25];
int num = 0;
//输入:
while(scanf("%s", tmp), tmp[0] != '#')
strcpy(str[num++], tmp);
//将字符串按字典序排:
qsort(str, num, sizeof(str[0]), cmp_str);
//全部处理成小写, 且其中的字符排成字典序, 放到word数组:
for(int i = 0; i < num; i++) {
int len = strlen(str[i]);
for(int j = 0; j < len; j++) {
if(str[i][j] >= 'A' && str[i][j] <= 'Z')
word[i][j] = str[i][j] + ('a' - 'A');
else
word[i][j] = str[i][j];
}
word[i][len] = '\0';
qsort(word[i], len, sizeof(char), cmp_char);
}
//判断是否出现, 不出现
for(int i = 0; i < num; i++) {
int count = 0;
for(int j = 0; j < num; j++) {
if(!strcmp(word[i], word[j]))
count++;
}
if(count == 1)
puts(str[i]);
}
return 0;
}
UVA 156 (13.08.04)的更多相关文章
- UVA 10474 (13.08.04)
Where is the Marble? Raju and Meena love to play with Marbles. They have got a lotof marbles with ...
- UVA 10194 (13.08.05)
:W Problem A: Football (aka Soccer) The Problem Football the most popular sport in the world (ameri ...
- UVA 253 (13.08.06)
Cube painting We have a machine for painting cubes. It is supplied withthree different colors: blu ...
- UVA 10790 (13.08.06)
How Many Points of Intersection? We have two rows. There are a dots on the toprow andb dots on the ...
- UVA 573 (13.08.06)
The Snail A snail is at the bottom of a 6-foot well and wants to climb to the top.The snail can cl ...
- UVA 10499 (13.08.06)
Problem H The Land of Justice Input: standard input Output: standard output Time Limit: 4 seconds In ...
- UVA 10025 (13.08.06)
The ? 1 ? 2 ? ... ? n = k problem Theproblem Given the following formula, one can set operators '+ ...
- UVA 465 (13.08.02)
Overflow Write a program that reads an expression consisting of twonon-negative integer and an ope ...
- UVA 10494 (13.08.02)
点此连接到UVA10494 思路: 采取一种, 边取余边取整的方法, 让这题变的简单许多~ AC代码: #include<stdio.h> #include<string.h> ...
随机推荐
- Two-stage rotation animation is deprecated. This application should use the smoother single-stage an
问题出在rootViewController同时包含UITabBarController和UINavigationController. 几经尝试,最后发现,在设置为window.rootViewCo ...
- Emacs 编辑verilog 学习日记
终于开始写博客啦.以前一直打算写.由于工作忙一次次延期了.写博客的好处不必多说. 以前有一些学习心得总是用一个word,或者note记录一些.时间久了都不知道弄哪儿去了.. 从今天开始记录学习中 ...
- Linux命令学习<不断更新>
没有系统的学习过Linux命令,遇到了就学习一下,慢慢积累. 1.echo 命令,学习网站『https://linux.cn/article-3948-1.html』. echo单词有回声.共鸣的意思 ...
- [BZOJ4487][JSOI2015]染色问题(容斥)
一开始写了7个DP方程,然后意识到这种DP应该都会有一个通式. 三个条件:有色行数为n,有色列数为m,颜色数p,三维容斥原理仍然成立. 于是就是求:$\sum_{i=0}^{n}\sum_{j=0}^ ...
- [SimpleOJ229]隧道
题目大意: 有10个格子,初始状态a和b分别在5和6上. 现在有n个任务,每个任务都有特定的位置. 在每个单位时间,a和b可以分别进行以下事件中的任意一件: 1.向左(右)移动一个格子: 2.锁定在当 ...
- bzoj 2844 子集异或和名次
感谢: http://blog.sina.cn/dpool/blog/s/blog_76f6777d0101d0mr.html 的讲解(特别是2^(n-m)的说明). /*************** ...
- rapidjson的使用
1.包含头文件 #include "rapidjson/stringbuffer.h"#include "rapidjson/document.h"#inclu ...
- Python知识(5)--绘图
Python学习变得很方便,不用任何安装一个浏览器也能够使用学习,比如Jupyter就是很好的一个网络工具,提供了编辑编译展示等强大的功能,网址如下: https://try.jupyter.org/ ...
- javaresource 红色X
出现这个问题的原因很多,解决办法也很多,我这里只记录我所遇到的. 这个问题一直存在,但是不影响项目运行. 后来在网上找了下资料,有一篇文章是让修改maven的settings.xml.将jdk1.6修 ...
- paypal对接
paypal支付接口准备工作 首先去申请一个paypal账号,https://www.paypal.com/. 申请完毕并登录,进入https://developer.paypal.com/devel ...