UVA 156-Ananagrams(字符串排序按序输出无重复单词)

Ananagrams

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld
& %llu

Submit Status

Description

 Ananagrams 

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

题意:把每个单词全部转化成小写字母,对每个单词,看它的字母重排后得到的单词在所有输入的单词中是否出现过,若没有出现,就输出原单词。所有要输出的单词按字典序排列输出。

思路:将所有输入单词存储并排序,将所有字母转化为小写另外存储,对另外存储的每个单词排序。再对另外存储并排序的单词搜一遍,看每个单词是否只出现一次,出现一次,就将对应的原单词输出。

PS:真心受不了,不会用sort对字符串排序,又重新学的用qsort对字符串排序。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
using namespace std;
char str[110][30];
char rep[110][30];
int cmp_char(const void *a,const void *b)//对字母进行排序
{
    return *(char *)a-*(char *)b;
}
int cmp_string(const void *a,const void *b)//对单词进行排序
{
    return strcmp((char *)a,(char *)b);
}
int main()
{
    int i,j;
    int cnt=0;
    int len;
    int sum;
    char a[30];
    while(~scanf("%s",a))
    {
        if(a[0]=='#')
            break;
        strcpy(str[cnt++],a);
    }
    qsort(str,cnt,sizeof(str[0]),cmp_string);
    memset(rep,0,sizeof(rep));
    for(i=0; i<cnt; i++)
    {
        len=strlen(str[i]);
        for(j=0; j<len; j++)
        {
            if(str[i][j]>='A'&&str[i][j]<='Z')
                rep[i][j]=str[i][j]+32;
            else
                rep[i][j]=str[i][j];
        }
        qsort(rep[i],len,sizeof(char),cmp_char);
    }
    for(i=0;i<cnt;i++)
    {
        sum=0;
        for(j=0;j<cnt;j++)
            if(strcmp(rep[i],rep[j])==0)
            sum++;
        if(sum==1)
            printf("%s\n",str[i]);
    }
    return 0;
}
时间: 2025-01-15 23:35:41

UVA 156-Ananagrams(字符串排序按序输出无重复单词)的相关文章

UVA 156 Ananagrams 关于二维数组表示的字符串排序的问题

题目链接:UVA 156 Ananagrams  Ananagrams  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

uva 156 - Ananagrams (反片语)

csdn:https://blog.csdn.net/su_cicada/article/details/86710107 例题5-4 反片语(Ananagrams,Uva 156) 输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文 本中的另外一个单词. 在判断是否满足条件时,字母不分大小写,但在输出时应保留输入中 的大小写,按字典序进行排列(所有大写字母在所有小写字母的前面). Sample Input ladder came tape soon leader ac

求字符串的最长无重复字符子串长度

题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3. 示例 2: 输入: "bbbbb" 输出: 1 解释: 因为无重复字符的最长子串是 "b",所以其长度为 1. 示例 3: 输入: "pwwkew" 输出: 3 解释: 因为无重复字符的最长子串是 "wk

查找字符串中最长无重复字符的子串

设定一个当前子字符串:tempString 设定一个保持最长无重复子串的数组:list 思路: 从第一个字符开始判断, 如果当前子串不包括当前的字符,则当前子串加入当前的字符成为新的当前子串, 如果当前子串包括当前的字符,判断当前字符在当前字符串中的位置,根据这个位置把字符串分成两个字符串,如果后面一个末尾加当前字符为新的当前子字符串,判断当前子串跟list数组中的子串长度,如果当前子串长,则清空list,把当前子串加入:如果相等,直接将当前子串加入list. 最后list的中的子字符串就是最长

字符串问题之 找到字符串的最长无重复子串

给定一个字符串str, 返回str的最长无重复字符子串长度 例如 str="abcd' 返回4 str="aabcb" 最长"abc" 返回3 解决本题的思路非常非常有趣,这种思路必须要学会: 本题目可以做到  时间复杂度O(N)  str长度N     空间复杂度O(M)  M是字符编码 根据字符编码 大小 申请 map  key表示字符  value表示最近出现的位置 int pre 遍历到的字符str[i] 以为str[i]结尾的最长无重复字符子串开

UVa 156 Ananagrams(STL,map)

 Ananagrams  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 let

字符串排序并输出

story='''I'm hurting, baby, I'm broken down I need your loving, loving, I need it now When I'm without you I'm something weak You got me begging Begging, I'm on my knees I don't wanna be needing your love I just wanna be deep in your love And it's ki

UVa 156 - Ananagrams

 Ananagrams  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 let

字符串练习(八):最长无重复字符子串

对于一个字符串,请设计一个高效算法,找到字符串的最长无重复字符的子串长度. 给定一个字符串A及它的长度n,请返回它的最长无重复字符子串长度.保证A中字符全部为小写英文字符,且长度小于等于500. 测试样例: "aabcb",5 返回:3 public class DistinctSubstring { public int longestSubstring(String A, int n) { // write code here if(A==null || n==0){ return