Ananagrams UVA 156

题目:

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

源代码:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAXN 1000+5
#define MAXL 80+5

char dic[MAXN][MAXL];//保存原始的单词
char word[MAXL];
char re_dic[MAXN][MAXL];//保存转化为小写字母并排序的单词
int cnt=0;

void word_insert();//获得单词,且有序的插入单词表
void re_order();//将re_dic中的每个单词内部排序

int main(){
 int i,j,k,flag;
// freopen("data","r",stdin);
 while(scanf("%s",word)&&word[0]!='#')
  word_insert();

 re_order();

 for(i=0;i<cnt;i++){
  if(strlen(dic[i])==1){//注意,一个字母的单词也为ananagram
   printf("%s\n",dic[i]);
   continue;
   }

  flag=0;
  for(j=0;j<cnt;j++){
   if(i==j)
   continue;

   if(strcmp(re_dic[i],re_dic[j])==0){//两个单词的re_dic相同,说明含有相同的字母组成,即为ananagram
    flag=1;
    break;
    }
   }
   if(!flag) printf("%s\n",dic[i]);
  }

 return 0;
}

void re_order(){
 int i,j,k;
 char temp;

 for(i=0;i<cnt;i++)//将re_dic里的单词内部排序,便于以后比较
  for(j=1;j<strlen(re_dic[i]);j++)
   for(k=j;k>0;k--)
    if(re_dic[i][k]<re_dic[i][k-1]){
      temp=re_dic[i][k];
      re_dic[i][k]=re_dic[i][k-1];
      re_dic[i][k-1]=temp;
    }
    else break;
}

void word_insert(){
 int i,j;
 if(cnt==0){
  strcpy(dic[0],word);
  strcpy(re_dic[0],word);
  cnt++;
  return;
  }

 for(i=cnt-1;i>=0;i--)//按字典序插入新读入的单词
  if(strcmp(dic[i],word)>0){
   strcpy(dic[i+1],dic[i]);
   strcpy(re_dic[i+1],re_dic[i]);
  }
  else break;

 strcpy(dic[i+1],word);

 for(j=0;j<strlen(word);j++)//题目要求不区分大小写
 if(isupper(word[j]))
  word[j]=tolower(word[j]);

 strcpy(re_dic[i+1],word);
 cnt++;

 return ;
}

Ananagrams UVA 156,布布扣,bubuko.com

时间: 2024-10-11 18:00:40

Ananagrams UVA 156的相关文章

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

STL语法——映射:map 反片语(Ananagrams,UVa 156)

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 lett

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

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

【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

UVa 156 (stl map的使用)

  一.map map就是从键(key)到值(value)的映射,重载了[]所以可以认为是高级版的数组,常用的一些操作如下: 头文件:#include<map> 定义:map <类型一(key),类型二(value)> name  key称为map的frist,value称为map的second. 初始化:name.clear(); 二.题目 Ananagrams Most crossword puzzle fans are used to anagrams--groups of

UVA 156(STL_G题)解题报告

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=92 -------------------------------------------------------------------------------------------------------------------------------------------

uva 156 (map)

暑假培训习题 1.用vector<string>储存string类型的输入单词: 2.将vector中的元素逐一标准化后映射进map中,并给map值加一: 3.新建一个空的vector 4.标准化之前的vector中的元素,并查看map中的值,如果是一,则把这个元素压入新的vector中,给新的vector排序并输出: #include<stdio.h>#include<map>#include<string>#include<algorithm>