结构之法-字符串及链表的探索-编程之美-第3章

编程之美第3章

3.3计算字符串的相似度

采用动态规划的方法实现代码如下

具体实现如下:

View Code
 1 /*--------------------------------------------------------------
 2  *name:计算字符串的相似度
 3  *data:2012-7-5
 4  *author:lp
 5  *introduction:利用动态规划对两个不同的字符串,判断其相似度
 6  *-------------------------------------------------------------*/
 7 #include<stdio.h>
 8 #include<string.h>
 9 #include<malloc.h>
10 int lena,lenb;//A,B两个字符串的长度
11 int min(int a,int b,int c)
12 {
13     int k=a;
14     if(k>b)k=b;
15     if(k>c)k=c;
16     return(k);
17 }
18 void main()
19 {
20     int i,j;
21     char a[]="abcd";
22     char b[]="bcef";
23     lena=strlen(a);
24     lenb=strlen(b);
25     //创建动态数组用于存储中间变量
26     int ** dist=(int **)malloc(sizeof(int *)*(lena+1));
27     for(i=0;i<lena+1;i++)
28         dist[i]=(int *)malloc(sizeof(int)*(lenb+1));
29     //dist数组的初始化
30     dist[0][0]=0;
31     for(i=1;i<lena+1;i++)
32         dist[i][0]=i;
33     for(j=1;j<lenb+1;j++)
34         dist[0][j]=j;
35     for(i=1;i<lena+1;i++)
36         for(j=1;j<lenb+1;j++)
37         {
38             if(a[i-1]==b[j-1])
39                 dist[i][j]=dist[i-1][j-1];
40             else
41                 dist[i][j]=min(dist[i-1][j-1],dist[i-1][j],dist[i][j-1])+1;
42         }
43     for(i=0;i<lena+1;i++)
44     {
45         for(j=0;j<lenb+1;j++)
46             printf(" %d ",dist[i][j]);
47         printf("\n");
48     }
49     printf("字符串a和字符串b的距离为:%d\n",dist[lena][lenb]);
50 }

 

 3.5 最短摘要的生成

  公司介绍包括M个字符串,给出N个单词关键字,目标是找出公司介绍中包含N个关键字的长度最短的字串。

具体实现如下:

View Code
 1 /*--------------------------------------------------------------------------------
 2  *name:最短摘要的生成
 3  *author:lp
 4  *data:2012-7-12
 5  *------------------------------------------------------------------------------*/
 6 #include<iostream>
 7 #include<string>
 8 using namespace std;
 9 bool isOneOf(string s1,string s2[],int n)//s1存在于s2[n]中的判断,存在返回1,否则返回0;
10 {
11     int k=0;
12     for(int i=0;i<n;i++)
13         if(s1==s2[i])
14         {
15             k++;
16             break;
17         }
18     if(k>0) return true;
19     else return false;
20 }
21 bool isAllExisted(string str[],string des[],int k,int lendes)//str[]中的元素全部在des[]中的判断
22 {
23     int i;
24     bool flag=true;
25     if(k!=lendes)
26         return false;
27     else
28     {
29         for(i=0;i<k;i++)
30             if(!isOneOf(str[i],des,k))
31             {
32                 flag=false;
33                 break;
34             }
35     }
36     return flag;    
37 }
38 void main()
39 {
40     string des[]={"微软","亚洲","计算机"};
41     string src[]={"微软","亚洲","研究院","成立","","1998","","","我们","","使命",
42     "","使","未来","","计算机","能够","","","","","","",
43     "","","自然语言","","人类","进行","交流","","","","基础","",
44     "","微软","亚洲","研究院","","","促进","计算机","","亚太","地区",
45     "","普及","","改善","亚太","用户","","计算","体验","",""};
46     //获取目标数组和摘要数组的长度
47     int lensrc,lendes;
48     lensrc=sizeof(src)/sizeof(string);
49     lendes=sizeof(des)/sizeof(string);
50     int ntargetlen=lensrc+1;
51     int pbegin=0;
52     int pend=0;
53     int nlen=lensrc;
54     int nabstractbegin=0;
55     int nabstractend=0;
56     string *str=new string[lendes];//用于存储临时
57     int i,k=0;
58     while(true)
59     {
60         while(!isAllExisted(str,des,k,lendes) && pend<nlen)//没有全部存在
61         {
62             if(isOneOf(src[pend],des,lendes) && (!isOneOf(src[pend],str,lendes)))//src[pend]为des[]中的元素,并且在str[]中不存在
63             {
64                 str[k++]=src[pend];
65             }
66             pend++;
67         }
68         while(isAllExisted(str,des,k,lendes))
69         {
70             if(pend-pbegin<ntargetlen)
71             {
72                 ntargetlen=pend-pbegin;
73                 nabstractbegin=pbegin;
74                 nabstractend=pend;
75             }
76 
77             if(isOneOf(src[pbegin],str,k))//增加判断src[pbegin]存在于str中
78             {
79                 for(i=0;i<k-1;i++)//每次修改str[],,
80                     str[i]=str[i+1];
81                 str[k-1]="";
82                 k--;
83             }
84             pbegin++;
85         }
86         if(pend>=lensrc)
87             break;
88     }
89     cout<<"最短摘要为:"<<endl;
90     for(i=nabstractbegin;i<nabstractend;i++)
91         cout<<src[i];
92     cout<<endl;
93     
94 }

 

3.8 求二叉树中节点的最大距离

posted @ 2012-07-11 11:56  lpshou  阅读(356)  评论(0编辑  收藏  举报