3.3 计算字符串的相似度
来源:互联网 发布:项目数据分析师认证 编辑:程序博客网 时间:2024/06/06 04:16
问题:
给定任意两个字符串,修改、增加、删除方式让他们变得相同,最少步骤。
解决:
递归解决,一步操作后,将下面三种情况变成相同字符串
1. A[2, ... , end] 和 B[1, ... , end]
2. A[1, ... , end] 和 B[2, ... , end]
3. A[2, ... , end] 和 B[2, ... , end]
代码如下:
#include <stdio.h>#include <stdlib.h>#include <string.h>int min(int t1, int t2, int t3){ int temp = 0; if (t1 > t2) temp = t2; else temp = t1; if(temp > t3) return t3; else return temp;}int calculateStrDistance(char* strA, int pABegin, int pAEnd, char* strB, int pBBegin, int pBEnd){ if(pABegin > pAEnd) { if(pBBegin > pBEnd) return 0; else return pBEnd - pBBegin + 1; } if(pBBegin > pBEnd) { if(pABegin > pAEnd) return 0; else return pAEnd - pABegin + 1; } if(strA[pABegin] == strB[pBBegin]) { return calculateStrDistance(strA, pABegin + 1, pAEnd, strB, pBBegin + 1, pBEnd); } else { int t1 = calculateStrDistance(strA, pABegin, pAEnd, strB, pBBegin + 1, pBEnd); int t2 = calculateStrDistance(strA, pABegin + 1, pAEnd, strB, pBBegin, pBEnd); int t3 = calculateStrDistance(strA, pABegin + 1, pAEnd, strB, pBBegin + 1, pBEnd); return min(t1, t2, t3) + 1; }}int main(){ char* strA = "abcdef"; char* strB = "abcdefg"; int pABegin = 0; int pAEnd = 5; int pBBegin = 0; int pBEnd = 6; printf("The distance is %d\n", calculateStrDistance(strA, pABegin, pAEnd, strB, pBBegin, pBEnd)); return 0;}
- 3.3 计算字符串的相似度
- 3.3计算字符串的相似度
- 3.3 计算字符串的相似度
- 计算字符串的相似度
- 计算字符串的相似度
- 计算字符串的相似度
- 计算字符串的相似度
- 计算字符串的相似度
- 计算字符串的相似度
- 计算字符串的相似度
- 计算字符串的相似度
- 计算字符串的相似度
- 计算字符串的相似度
- 计算字符串的相似度
- 计算字符串的相似度
- 计算字符串的相似度
- 计算字符串的相似度。
- 计算字符串的相似度
- 常用标点符号中英文对照表
- Ubuntu安装搜狗拼音输入法-sogoupinyin
- Android Handler 异步消息机制
- php 学习记录 formdata 文件上传 ajax html5
- JRTSP相关
- 3.3 计算字符串的相似度
- webservice中参数类型为datatable,报错“生成 XML 文档时出错”
- LaTeX :Step By Step (1)运行LaTeX
- 谷歌Barra因感情辞职,揭晓这位神秘的女人!
- php学习记录 html5 createObjectURL
- C语言数据结构----递归的应用(八皇后问题的具体流程)
- poj - 2159 - Ancient Cipher(统计)
- BAE WordPress安装主题后SVN找不到主题文件
- 3DShader之投影贴图(Projective Texturing)