【华为机试在线训练】Day 9

公共字串计算

题目描述

题目标题:

计算两个字符串的最大公共字串的长度,字符不区分大小写

详细描述:

接口说明

原型:

int getCommonStrLength(char * pFirstStr, char * pSecondStr);

输入参数:

     char * pFirstStr //第一个字符串

     char * pSecondStr//第二个字符串

输入描述:

输入两个字符串

输出描述:

输出一个整数

示例1

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <vector>
#include<algorithm>
using namespace std;
int main()
{
  
    string str1,str2;
    while(cin>>str1)
    {
        cin>>str2;
        vector<vector<int> > matrix(str1.size(),vector<int>(str2.size()));
        int max_num=0;
        for(int i=0;i<str1.size();i++)
        {
            for(int j=0;j<str2.size();j++)
            {
                if(str1[i]!=str2[j])
                    matrix[i][j]=0;
                else if(i==0||j==0)
                {
                    matrix[i][j]=1;
                    if(max_num<1)
                        max_num=1;
                }
                else
                {
                    matrix[i][j]=matrix[i-1][j-1]+1;
                    if(matrix[i][j]>max_num)
                        max_num=matrix[i][j];
                }
            }
        }
        cout<<max_num<<endl;
  
    }
    return 0;
}


计算字符串的相似度

题目描述

对于不同的字符串,我们希望能有办法判断相似程度,我们定义了一套操作方法来把两个不相同的字符串变得相同,具体的操作方法如下:

1 修改一个字符,如把“a”替换为“b”。

2 增加一个字符,如把“abdd”变为“aebdd”。

3 删除一个字符,如把“travelling”变为“traveling”。

比如,对于“abcdefg”和“abcdef”两个字符串来说,我们认为可以通过增加和减少一个“g”的方式来达到目的。上面的两种方案,都只需要一次操作。把这个操作所需要的次数定义为两个字符串的距离,而相似度等于“距离+1”的倒数。也就是说,“abcdefg”和“abcdef”的距离为1,相似度为1/2=0.5.

给定任意两个字符串,你是否能写出一个算法来计算出它们的相似度呢?

请实现如下接口

 /* 功能:计算字符串的相似度
  * 输入:pucAExpression/ pucBExpression:字符串格式,如: "abcdef"
  * 返回:字符串的相似度,相似度等于“距离+1”的倒数,结果请用1/字符串的形式,如1/2
  */
 public static  String  calculateStringDistance(String expressionA, String expressionB)
 {
     /* 请实现*/
     return null;
 }

约束:

1、PucAExpression/ PucBExpression字符串中的有效字符包括26个小写字母。

2、PucAExpression/ PucBExpression算术表达式的有效性由调用者保证;
3、超过result范围导致信息无法正确表达的,返回null。

输入描述:

输入两个字符串

输出描述:

输出相似度,string类型

这道题其实简单的理解就是为了求字符串的最小编辑代价,也是一个经典的动态规划题,复杂度O(M*N)
思路:1.求解状态转移矩阵dp[M + 1][N + 1],dp[i][j] 的值代表的是str1[0...i-1]编辑为str2[0...j-1]
的最小代价。
2. 计算过程:
1)dp[0][0] = 0,表示str1空的字串编辑为str2空的字串代价为0。
2)矩阵dp第一列即为dp[0...M-1][0],dp[i][0] 表示str1[0...i-1]编辑为空串的最小代价,所以就是将str1[0..M-1]的字符删掉的代价
所以dp[i][0] = i;
3) 同2),那str2[0...j-1]编辑的代价,dp[0][j] = j;
4) 接下来的位置就按照从左到右,从上到下来计算,dp[i][j]的值来至于下面的几种情况:
(1)str1[0...i-1]可以先编辑为str1[0..i-2],也就是删除字符str1[i-1],然后由str1[0..i-2]编辑为str2[0...j-1],dp[i-1][j]表示str1[0..i-2]编辑为str2[0...j-1]的最小代价,
那么dp[i][j]可能等于dp[i -1][j] + 1;
(2)str1[0...i-1]可以先编辑为str1[0..i-2],然后将str2[0..j-2]插入字符str2[j-1],编辑成str2[0...j-1],dp[i][j-1]表示str1[0..i-1]编辑成str2[0...j-2]的最小代价,
那么dp[i][j] 可能等于dp[i][j-1] + 1;
(3) 如果str1[i - 1]!=str2[j-1] ,那么先把str1[0..i-1]中的str1[0..i-2]的部分边长str2[0..j-2],然后把字符str1[i-1]替换为str2[j-1],这样str1[0..i-1]就编辑成为str2[0...j-1]了,dp[i - 1][j - 1]表示
str1[0..i-2]编辑为str2[0..j-2]的最小代价,那么dp[i ][j]可能等于dp[i - 1][j - 1] + 1;
(4) 如果str1[i - 1]==str2[j-1] ,那么先把str1[0..i-1]中的str1[0..i-2]的部分边长str2[0..j-2],因为此时   str1[i - 1]==str2[j-1] ,所以str1[0..i-1]已经编辑为str2[0..j-1]了,dp[i - 1][j - 1]表示str1[0..i-2]编辑为str2[0..j-2]的最小代价, 那么dp[i ][j]可能等于dp[i - 1][j - 1]。
 
上述的4中情况取最小值,dp的最右下角就是最终结果,即最小编辑代价。

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str1 = sc.next();
String str2 = sc.next();
System.out.println(calculateStringDistance(str1,str2));
}
}
private static String calculateStringDistance(String
  str1, String str2){
if(str1 == null || str2 == null || str1.length() <= 0
  || str2.length() <= 0)
return null;
char[] chs1 = str1.toCharArray();
char[] chs2 = str2.toCharArray();
int dp = getDp(chs1,chs2);
System.out.println(dp);
return String.valueOf("1/" + (dp + 1));
}
private static int getDp(char[] chs1,char[] chs2){
int[][]dp = new int[chs1.length + 1][chs2.length + 1];
for(int i = 1;i<=chs1.length;i++){
dp[i][0] = i;
}
for(int j = 1;j<=chs2.length;j++){
dp[0][j] = j;
}
for(int i = 1;i<=chs1.length;i++){
for(int j = 1;j<=chs2.length;j++){
if(chs1[i - 1] == chs2[j - 1]){
dp[i][j] = dp[i - 1][j - 1];
}else{
dp[i][j] = dp[i - 1][j - 1] + 1;
}
dp[i][j] = Math.min(dp[i][j], dp[i][j - 1] + 1);
dp[i][j] = Math.min(dp[i][j], dp[i - 1][j] + 1);
}
}
return dp[chs1.length][chs2.length];
}
}
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
 
int main(){
    string a,b;
     
    while(cin>>a>>b){
         string res="1/";
         int n = (int)a.size(),m = (int)b.size();
         vector<vector<int>>dp(n+1,vector<int>(m+1,0));
         dp[0][0] = 0;//dp[x][y]代表将a字符串前x个字符修改成b字符串前y个字符
         for (int i=1; i<=m; ++i) dp[0][i] = i;
         for (int i=1; i<=n; ++i) dp[i][0] = i;
         for (int i=1; i<=n; ++i) {
            for (int j=1; j<=m; ++j) {
                 int one = dp[i-1][j] +1,two = dp[i][j-1]+1,three = dp[i-1][j-1];
                 if(a[i-1]!=b[j-1]) three+=1;
                     dp[i][j] = min(min(one,two),three);
             }
         }
         
         res+=to_string(1+dp[n][m]);
 
         cout<<res<<endl;
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/HelloZEX/article/details/81484175