第二章 啊哈!算法(变位词)

给定一个英语字典,找出其中的所有变位词集合。例如,“pots”、“stop”和“tops”互为变位词,因为每一个单词都可以通过改变其他单词中字母的顺序来得到。

“解决这个问题的许多方法都出奇地低效和复杂。任何一种考虑单词中所有字母的排列的方法都注定了要失败。而比较所有单词对的任何方法在我的机器上运行至少要花费一整夜的时间。”

我们获得的”啊哈!灵机一动“就是标识字典中的每一个词,使得在相同变位词类中的单词具有相同的标识。然后,将所有具有相同标识的单词集中在一起。这将原始的变位词问题简化为两个子问题:选择标识和集中具有相同标识的单词。

对第一个问题,我们可以使用基于排序的标识:将单词中的字母按照字母表顺序排列。要解决第二个问题,我们将所有的单词按照其标识的顺序排列。

书中的程序按照三阶段的”管道“组织,其中一个程序的输出文件作为下一个程序的输入文件。第一个程序sign()直接写在main函数里来标识单词,第二个程序排序sort()标识后的文件,而第三个程序squash()将这些单词压缩为每个变位词类一行的形式。

程序有一些局限在于要事先输入单词的个数,以for循环让输入停止,而且输入单词的长度不能长于20位。

[cpp]  view plain copy
  1. // 实现变位词程序  
  2.   
  3. #include <stdio.h>  
  4. #include <stdlib.h>  
  5. #include <string.h>  
  6.   
  7. #define MAX 20  
  8.    
  9. char *wordlist[MAX];  
  10. char *signlist[MAX];  
  11. //static int n;  
  12.   
  13. int charcmp(const void *x, const void *y)  
  14. {  
  15.     return *(char *)x-*(char *)y;  
  16. }   
  17.   
  18. int stringcmp(const void *x, const void *y)  
  19. {  
  20.     return strcmp((char *)x,(char *)y);  
  21. }  
  22.   
  23. void sort(int num)  
  24. {  
  25.      int i,j;  
  26.      char *temp1=NULL, *temp2=NULL;  
  27.      //int n=sizeof(signlist)/sizeof(signlist[0]);  
  28.      //printf("%d\n",num);  
  29.      //qsort(signlist, num, sizeof(char *), stringcmp);  
  30.        
  31.      //交换法排序  
  32.        
  33.      for(i=0;i<num;i++)  
  34.      {  
  35.                        for(j=i+1;j<num;j++)  
  36.                        {  
  37.                                            if(strcmp(signlist[j],signlist[i])<0)  
  38.                                            {  
  39.                                                                               temp1=signlist[i];  
  40.                                                                               signlist[i]=signlist[j];  
  41.                                                                               signlist[j]=temp1;  
  42.                                                                               temp2=wordlist[i];  
  43.                                                                               wordlist[i]=wordlist[j];  
  44.                                                                               wordlist[j]=temp2;  
  45.                                            }  
  46.                        }  
  47.      }  
  48.        
  49.      for(i=0;i<num;i++)  
  50.          printf("%s %s\n",signlist[i],wordlist[i]);  
  51. }  
  52.   
  53. void squash(num)  
  54. {  
  55.      int i;  
  56.      //char *Old=NULL, *New=NULL;  
  57.      char Old[MAX], New[MAX];  //利用字符数组而不是字符指针   
  58.      for(i=0;i<num;i++)  
  59.      {  
  60.                        if(i==0)  
  61.                        {  
  62.                               strcpy(Old,signlist[i]);  
  63.                               //strcpy(Old,signlist[i]);  
  64.                               //Old=signlist[i];  
  65.                               printf("%s ",wordlist[i]);  
  66.                        }   
  67.                        else  
  68.                        {  
  69.                             strcpy(New,signlist[i]);  
  70.                             //New=signlist[i];  
  71.                             if(strcmp(New,Old)==0)  
  72.                                 printf("%s ",wordlist[i]);  
  73.                             else  
  74.                             {  
  75.                                 printf("\n");  
  76.                                 strcpy(Old,signlist[i]);  
  77.                                 //Old=signlist[i];  
  78.                                 printf("%s ",wordlist[i]);  
  79.                             }  
  80.                        }  
  81.      }  
  82. }  
  83.   
  84. // 输入一组单词,产生字典序标识的单词组   
  85. // scanf()按s格式符将字符串作为一个整体输入/输出:空格、回车或跳格(Tab)符作为输入数据的分隔符,因而不能被  
  86. // 读入,输入遇到这些字符时,系统认为输入结束   
  87. int main()  
  88. {  
  89.     int n;  
  90.     char word[MAX], sign[MAX];  
  91.     //int i=j=0; 首先是输入然后转化为sign()  
  92.     int i,j;  
  93.     printf("Please input how many words are you going to input:");  
  94.     scanf("%d",&n);  
  95.     printf("Please input the words :\n");  
  96.     //while(scanf("%s",&word)!=EOF)  
  97.     for(i=0;i<n;i++)  
  98.     {  
  99.                                  //scanf("%s",wordlist[i]);  
  100.                                  scanf("%s",&word);  
  101.                                  wordlist[i]=(char *)malloc(sizeof(word));  
  102.                                  strcpy(wordlist[i],word);  
  103.                                  //strcpy(wordlist[i],word); 指针没有指向固定的位置   
  104.                                  //wordlist[i]=word;  
  105.                                  //strcpy(word,)  
  106.                                  strcpy(sign, word);  
  107.                                  qsort(sign, strlen(sign), sizeof(char), charcmp);  
  108.                                  signlist[i]=(char *)malloc(sizeof(sign));  
  109.                                  strcpy(signlist[i],sign);  
  110.                                  //signlist[i]=sign;  
  111.                                  //strcpy(signlist[i],sign);  
  112.                                  //qsort(word, sizeof(word), sizeof(char), charcmp);  //指针没有指向固定的位置   
  113.                                  //strcat(orderword[i], word);  
  114.                                  //i++;  
  115.                                  //printf("%s %s\n", word, sig);  
  116.                                  printf("%s %s\n",wordlist[i],signlist[i]);  
  117.                                  //i++;  
  118.     }  
  119.     printf("\n");  
  120.     //for(j=0;j<n;j++)  
  121.         //printf("%s %s\n",wordlist[j],signlist[j]);//  
  122.     /* 
  123.     while(strcmp(orderword[j],"\0")) 
  124.     { 
  125.                                     printf("%s\n",orderword[j]); 
  126.                                     j++; 
  127.     } 
  128.     */  
  129.     sort(n);  
  130.     //printf("%s %s\n",wordlist[i],signlist[i]);  
  131.     printf("\n");  
  132.     squash(n);  
  133.     for(i=0;i<n;i++)  
  134.     {  
  135.                     free(wordlist[i]);  
  136.                     free(signlist[i]);  
  137.     }  
  138.     return 0;  
  139. }  
  140.   
  141. /* Test for the strcpy function 
  142. int main() 
  143. { 
  144.     char word[MAX], *p; 
  145.     scanf("%s", word); 
  146.     //strcpy(p,word); 
  147.     p=word; 
  148.     printf("%s", p); 
  149.     //printf("%c",*p); 
  150.      
  151.     return 0; 
  152. } 
  153. */  
  154. // Test for the sort algorithm  
  155. /* 
  156. int main() 
  157. { 
  158.     int i; 
  159.     char *word[]={"Pascal","Basic","Fortran","Java","Visiual C"}; //不需要定义数组的大小  
  160.     int n=sizeof(word)/sizeof(word[0]); 
  161.     printf("%d\n",n); 
  162.     qsort(word, n, sizeof(char *), stringcmp); 
  163.      for(i=0;i<n;i++) 
  164.          printf("%s\n",word[i]); 
  165.     return 0; 
  166.  
  167. */  

效果如下:


参考资料:

qsort()在字符指针数组中的应用:

http://www.cnblogs.com/2011-9-13/archive/2011/11/13/2247349.html

http://www.cppblog.com/Joe/archive/2010/10/29/131746.aspx?opt=admin

字符串排序:《C语言使用教程》


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值