字符串的修改

字符串的修改
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Total submit users: 235, Accepted users: 221
Problem 10411 : No special judgement
Problem description

设A和B是两个字符串。我们要用最少的字符操作次数,将字符串A转换为字符串B。这里所说的字符操作共有三种:

1. 删除一个字符;
2. 插入一个字符;
3. 将一个字符改为另一个字符。
对任给的两个字符串A和B,计算出将字符串A变换为字符串B所用的最少字符操作次数。

Input
第一行为字符串A;第二行为字符串B;字符串A和B的长度均小于200。

Output
只有一个正整数,为最少字符操作次数。

Sample Input
sfdxbqw
gfdgw
Sample Output
4
Problem Source
HNU Contest 

 

 1 #include<iostream>
 2 #include<iomanip>
 3 #define MAX(a,b) a>b?a:b
 4 #define max 100
 5 using namespace std;
 6 
 7 int LCSLength( int m , int n , char *x , char *y , char b[][max] )
 8 {
 9 int i , j ,k;
10 int c[max][max];
11 
12 for( i = 1 ; i <= m ; i++ )
13 {
14     c[i][0] = 0;
15 }
16 for( i = 1 ; i <= n ; i++ )
17 {
18     c[0][i] = 0;
19 }
20 
21 for( i = 1 ; i <= m ; i++ )
22 {
23     for( j = 1 ; j <= n ; j++ )
24       {
25            
26            if( x[i-1] == y[j-1] )
27                 {
28                     c[i][j] = c[i-1][j-1] + 1;               
29                     b[i][j] = '\\';
30                     
31                       }
32           else if( c[i-1][j] >= c[i][j-1] )         
33                  {
34                     c[i][j] = c[i-1][j];
35                     b[i][j] = '|';
36                    
37                      }
38           else
39                  {
40                        c[i][j] = c[i][j-1];
41                        b[i][j] = '-';
42                      
43                      }
44       }        
45     
46   } 
47    k=c[m][n];
48    return k;
49 }
50 
51 int main()
52 {
53     char s1[100001],s2[100001];
54     int n,m,len;
55     while(cin>>s1>>s2)
56     {    
57         m=strlen(s1);
58         n=strlen(s2);
59         len=MAX(n,m);
60         char b[max][max] = {0};
61         if(strcmp(s1,s2)==0) printf("%d\n",n-m);
62         else printf("%d\n",len-LCSLength( m , n , s1 , s2 , b ));
63     }
64 return 0;
65 }

转载于:https://www.cnblogs.com/hncu1009302xudandan/archive/2012/07/19/2600007.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值