首页 > 试题广场 >

相似单词变换

[问答题]
相似单词变换

题目描述:英文单词有很多非常相似,比如:see和seek、cat和cut等,现在提供3种编辑操作:insert、remove、replace,通过在单词1上进行这些操作,可以让单词1变成单词2

那么问题来了,如何只用最小次数的编辑操作,可以让字符串1变成字符串2?

说明:

1)3种编辑操作的代价是一样的

2)并且每次只能操作一个字符串的一个字母

3)只需要考虑在字符串1上进行编辑操作即可

输入

输入一行,有两个字符串,以空格分隔。

输出

输出为最小编辑次数。

样例输入

geek gesek

样例输出

1

#include <string>
#include<algorithm>
#include <vector>
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	string str1, str2;
	while (cin >> str1 >> str2){
		int len1 = str1.size();
		int len2 = str2.size();
		vector<vector<int>> dp(len1 + 1, vector<int>(len2 + 1,0));
		for (int i = 0; i <= len1; i++){
			dp[i][0] = i;
		}
		for (int j = 0; j <= len2; j++){
			dp[0][j] = j;
		}
		for (int i = 1; i <= len1; i++){
			for (int j = 1; j <= len2; j++){
				if (str1[i - 1] == str2[j - 1])
					dp[i][j] = dp[i - 1][j - 1];
				else
					dp[i][j] = min(min(dp[i - 1][j] + 1, dp[i][j - 1] + 1), dp[i - 1][j - 1] + 1);
			}
		}
		cout << dp[len1][len2] << endl;
	}
	return 0;
}

发表于 2017-09-09 17:09:31 回复(0)
# coding:utf8
def count_opera():
	hey = raw_input()
	while (not hey or len(hey.split(' '))!=2):
		print "Input Correct String"
		hey = raw_input()
	need_change, porpose = hey.split(' ')
	need_change_length = len(need_change)
	porpose_length = len(porpose)
	if need_change_length == porpose_length:
		print same_length(need_change, porpose)
	if need_change_length < porpose_length:
		print need_insert(need_change, porpose)
	if need_change_length > porpose_length:
		print need_delete(need_change, porpose)
	
def same_length(string_A, string_B):
	count = 0
	for index in xrange(len(string_B)):
		if string_A[index] != string_B[index]:
			count += 1
	return count

def need_insert(string_A, string_B):
	count = 0
	a_list = list(string_A)
	for index in xrange(len(string_B)):
		if a_list[index] != string_B[index]:
			a_list.insert(index, string_B[index])
			count += 1
		if len(a_list) == len(string_B):
			string_A = ''.join(a_list)
			break
	change_cnt = same_length(string_A, string_B)
	count += change_cnt
	return count
			
def need_delete(string_A, string_B):
	count = 0
	a_list = list(string_A)
	while (len(a_list) != len(string_B)):
		a_list = delete_and_judge(a_list, string_B)
		count += 1
	string_A = ''.join(a_list)
	change_cnt = same_length(string_A, string_B)
	count += change_cnt
	return count

def delete_and_judge(a_list, string_B):
	length = len(a_list)
	for index in xrange(length):
		if a_list[index] != string_B[index]:
			del a_list[index]
			return a_list

if __name__ == "__main__":
	count_opera()

发表于 2017-08-02 10:58:52 回复(0)
应该就是将两个字符串中较长的长度减去两个字符串的最长公共子序列,因为公共子序列不需要操作,剩下的可以全部看成replace操作,剩下的长度不同时,可以看成空格,比如geek -> gesek 的最长公共子序列为geek,剩余的为'sp'(space)->s,主需要一次replace操作
发表于 2018-04-18 16:38:00 回复(0)
 #include<iostream>
#include<string.h>
#include<algorithm>
#include<cmath> 
using namespace std;  
struct node  
{  
   int yuwen,shuxue,yingyu;   
   int num,sum;     
}student[3000]; 
 
int cmp(node x,node y)  
{  
    if(x.sum!=y.sum) return x.sum>y.sum;  
    if(x.yuwen!=y.yuwen) return x.yuwen>y.yuwen;  
    if(x.num!=y.num) return x.num<y.num;  
}  
int main()  
{  
    int i,j,n,m;  
    cin>>n; 
    for(i=0;i<n;i++)  
    {  
      cin>>student[i].yuwen>>student[i].shuxue>>student[i].yingyu;  
      student[i].num=i+1;  
      student[i].sum=student[i].yuwen+student[i].shuxue+student[i].yingyu;  
    }  
    sort(student,student+n,cmp);  
    for(i=0;i<5;i++)  
    cout<<student[i].num<<" "<<student[i].sum<<endl;  
  
    return 0;  


发表于 2018-02-01 22:11:20 回复(0)
#include<iostram>
#include<vector>
#include<string>
using namespace std;
int getsize(const string& str1,const string& str2,int pos1,int pos2,vector<vector<int>>& mark;)
{
 int result=10000;
    int size1=str1.size();
    int size2=str2.size();
 if(mark[pos1][pos2]!=-1)
 {
  return mark[pos1][pos2];
 }
 if(pos1==size1||pos2==size2)
 {
  if(pos1==0)
   return size2-pos2;
  else
   return size1-pos1;
 }
 
    if(str1[pos1]==str[pos2])
    {
  int tmp=getsize(str1,str2,pos1+1,pos2+1);
  if(result>tmp)
   result=tmp;
 }
 else
 {
 // if(size1-pos1>=size2-pos2)
  //{
   int tmp=1+ getsize(str1,str2,pos1+1,pos2);
   if(result>tmp)
    result=tmp;
 // }
  //if(size1-pos1<=size2-pos2)
  //{
   tmp=1+ getsize(str1,str2,pos1,pos2+1);
   if(result>tmp)
    result=tmp;
  //}
 }
 mark[pos1][pos2]=result;
 return result;
}
int main()
{
 string str1,str2;
 cin>>str1>>str2;
 //int mark[str1.size()+1][str2.size()+1]={0};
 vector<vector<int>> mark(str1.size()+1,vector<int>(str2.size(),-1)+1);
 int result=getsize(str1,str2);
 cout<<result<<endl;
 return 1;
}
发表于 2017-08-02 19:53:32 回复(0)