当前位置: 首页 > news >正文

南京做网站优化哪家好爱站关键词查询

南京做网站优化哪家好,爱站关键词查询,建设网站开发,wordpress密码文件夹题目 题目链接: https://www.nowcoder.com/practice/6a1483b5be1547b1acd7940f867be0da 思路 编辑距离问题 什么是两个字符串的编辑距离(edit distance)?给定字符串s1和s2,以及在s1上的如下操作:插入&…

题目

在这里插入图片描述
题目链接:
https://www.nowcoder.com/practice/6a1483b5be1547b1acd7940f867be0da

思路

编辑距离问题
什么是两个字符串的编辑距离(edit distance)?给定字符串s1和s2,以及在s1上的如下操作:插入(Insert)一个字符
移除(Remove)一个字符
替换(Replace)一个字符
试问最小需要多少次这样的操作才能使得s1转换为s2?
比如,单词“cat”和“hat”,这样的操作最少需要一次,只需要把“cat”中的“c”替换为“h”即可。
单词“recall”和“call”,这样的操作最少需要两次,只需要把“recall”中的“r”和“e”去掉即可。
单词“Sunday”和“Saturday”,这样的操作最少需要3次,在“Sunday”的“S”和“u”中插入“a”和“t”,
再把“n”替换成“r”即可。那么,是否存在一种高效的算法,能够快速、准确地计算出两个字符串的编辑距离呢?动态规划算法我们使用动态规划算法(Dynamic Programming)来计算出两个字符串的编辑距离。我们从两个字符串s1和s2的最末端向前遍历来考虑。假设s1的长度为m,s2的长度为n,算法如下:如果两个字符串的最后一个字符一样,那么,我们就可以递归地计算长度为m-1和n-1的两个字符串的情形;
如果两个字符串的最后一个字符不一样,那么,进入以下三种情形:
插入: 递归地计算长度为m和n-1的两个字符串的情形,
这是因为在s1中的末端插入了一个s2的最后一个字符,这样s1和s2的末端字符一样,就是1中情形;
删除: 递归地计算长度为m-1和n的两个字符串的情形,这是在s1中的末端删除了一个字符;
替换: 递归地计算长度为m-1和n-1的两个字符串的情形,
这是因为把s1中末端字符替换成了s2的最后一个字符,这样s1和s2的末端字符一样,就是1中情形;这样,我们就有了子结构问题。对于动态规划算法,我们还需要一个初始化的过程,
然后中间维护一张二维表即可。初始化的过程如下: 如果m为0,则至少需要操作n次,
即在s1中逐个添加s2的字符,一共是n次;如果n为0,则至少需要操作m次,
即把s1的字符逐个删除即可,一共是m次。

参考文档:https://www.cnblogs.com/jclian91/p/10184039.html
本答案采用递归实现,注意必须用缓存,否则时间复杂度过大

参考答案Java

import java.util.*;public class Solution {/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可*** @param str1 string字符串* @param str2 string字符串* @return int整型*/public int editDistance (String str1, String str2) {//动态规划:样本对应模型int n = str1.length();int m = str2.length();int[][] dp = new int[n + 1][m + 1];for (int i = 0; i <= n; i++) {for (int j = 0; j <= m; j++) {dp[i][j] = -1;}}return dfs(str1, str2, n, m, 1, dp);}public int dfs(String str1, String str2, int i, int j, int c, int[][] dp) {if (dp[i][j] != -1)return dp[i][j];int ans = 0;if (i == 0 && j == 0) ans = 0;else if (i == 0) ans = c * j;else if (j == 0) ans = c * i;else {ans = dfs(str1, str2, i - 1, j - 1, c,dp) + ((str1.charAt(i - 1) == str2.charAt(j - 1)) ? 0 : c);int ans1 = dfs(str1, str2, i, j - 1, c, dp) + c;if (ans > ans1) ans = ans1;int ans2 = dfs(str1, str2, i - 1, j, c, dp) + c;if (ans > ans2)ans = ans2;}dp[i][j] = ans;return ans;}
}

参考答案Go

package main/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可** * @param str1 string字符串 * @param str2 string字符串 * @return int整型
*/
func editDistance( str1 string ,  str2 string ) int {//动态规划:样本对应模型n := len(str1)m := len(str2)dp := make([][]int, n+1)for i := 0; i <= n; i++ {dp[i] = make([]int, m+1)for j := 0; j <= m; j++ {dp[i][j] = -1}}return dfs(str1, str2, n, m, 1, dp)
}func dfs(s1 string, s2 string, i int, j int, cnt int, dp [][]int) int {if dp[i][j] != -1 {return dp[i][j]}var ans int = 0if i == 0 && j == 0 {ans = 0} else if i == 0 {ans = j * cnt} else if j == 0 {ans = i * cnt} else {cur := cntif s1[i-1] == s2[j-1] {cur = 0}ans = dfs(s1, s2, i-1, j-1, cnt, dp) + curans1 := dfs(s1, s2, i, j-1, cnt, dp) + cntif ans > ans1 {ans = ans1}ans2 := dfs(s1, s2, i-1, j, cnt, dp) + cntif ans > ans2 {ans = ans2}}dp[i][j] = ansreturn ans
}

参考答案PHP

<?php/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可** * @param str1 string字符串 * @param str2 string字符串 * @return int整型*/
function editDistance( $str1 ,  $str2 )
{//动态规划:样本对应模型$n = strlen($str1);$m = strlen($str2);$dp = [];for ($i = 0; $i <= $n; $i++) {for ($j = 0; $j <= $m; $j++) {$dp[$i][$j] = -1;}}return dfs($str1, $str2, $n, $m, 1, $dp);
}function dfs($s1, $s2, $i, $j, $cnt, &$dp)
{if ($dp[$i][$j] != -1) {return $dp[$i][$j];}$ans = 0;if ($i == 0 && $j == 0) $ans = 0;else if ($i == 0) $ans = $j * $cnt;else if ($j == 0) $ans = $i * $cnt;else {$cur = $s1[$i - 1] == $s2[$j - 1] ? 0 : $cnt;$ans = dfs($s1, $s2, $i - 1, $j - 1, $cnt, $dp) + $cur;$ans1 = dfs($s1, $s2, $i, $j - 1, $cnt, $dp)+$cnt;if ($ans > $ans1) $ans = $ans1;$ans2 = dfs($s1, $s2, $i - 1, $j, $cnt, $dp)+$cnt;if ($ans > $ans2)$ans = $ans2;}$dp[$i][$j] = $ans;return $ans;}

文章转载自:
http://triennially.qwfL.cn
http://pickerelweed.qwfL.cn
http://penny.qwfL.cn
http://roup.qwfL.cn
http://berm.qwfL.cn
http://unlicensed.qwfL.cn
http://houndfish.qwfL.cn
http://lacey.qwfL.cn
http://embryectomy.qwfL.cn
http://vermifuge.qwfL.cn
http://pirimicarb.qwfL.cn
http://aliasing.qwfL.cn
http://syllogize.qwfL.cn
http://sigillography.qwfL.cn
http://heartless.qwfL.cn
http://groan.qwfL.cn
http://soogee.qwfL.cn
http://pulpitry.qwfL.cn
http://ozarkian.qwfL.cn
http://sporter.qwfL.cn
http://astrict.qwfL.cn
http://compulsionist.qwfL.cn
http://mow.qwfL.cn
http://flummox.qwfL.cn
http://tatouay.qwfL.cn
http://whine.qwfL.cn
http://considering.qwfL.cn
http://liegeman.qwfL.cn
http://sulphonamide.qwfL.cn
http://boatable.qwfL.cn
http://aonb.qwfL.cn
http://reproacher.qwfL.cn
http://overestimate.qwfL.cn
http://gear.qwfL.cn
http://fenderless.qwfL.cn
http://toadstone.qwfL.cn
http://eremacausis.qwfL.cn
http://multiplicate.qwfL.cn
http://andromonoecism.qwfL.cn
http://zoochore.qwfL.cn
http://wilily.qwfL.cn
http://craniectomy.qwfL.cn
http://leiden.qwfL.cn
http://elliptical.qwfL.cn
http://epidermolysis.qwfL.cn
http://posttonic.qwfL.cn
http://impersonalization.qwfL.cn
http://energic.qwfL.cn
http://eta.qwfL.cn
http://tolley.qwfL.cn
http://swanky.qwfL.cn
http://vfw.qwfL.cn
http://factitive.qwfL.cn
http://astrogator.qwfL.cn
http://slipware.qwfL.cn
http://phagosome.qwfL.cn
http://bine.qwfL.cn
http://fuzznuts.qwfL.cn
http://kerulen.qwfL.cn
http://blackhearted.qwfL.cn
http://corkboard.qwfL.cn
http://favorer.qwfL.cn
http://double.qwfL.cn
http://excarnate.qwfL.cn
http://folsom.qwfL.cn
http://filings.qwfL.cn
http://nitroaniline.qwfL.cn
http://sawbuck.qwfL.cn
http://missourian.qwfL.cn
http://synoptical.qwfL.cn
http://vicarious.qwfL.cn
http://offing.qwfL.cn
http://galvanography.qwfL.cn
http://indra.qwfL.cn
http://cheerful.qwfL.cn
http://three.qwfL.cn
http://joyo.qwfL.cn
http://triangulable.qwfL.cn
http://hermitry.qwfL.cn
http://phytolith.qwfL.cn
http://underclass.qwfL.cn
http://debited.qwfL.cn
http://microwatt.qwfL.cn
http://roul.qwfL.cn
http://polyphone.qwfL.cn
http://ventricle.qwfL.cn
http://precarious.qwfL.cn
http://patan.qwfL.cn
http://factionalize.qwfL.cn
http://vagus.qwfL.cn
http://trigoneutic.qwfL.cn
http://parsimonious.qwfL.cn
http://expositive.qwfL.cn
http://wonderland.qwfL.cn
http://woolly.qwfL.cn
http://subvocal.qwfL.cn
http://numbfish.qwfL.cn
http://foothot.qwfL.cn
http://lanugo.qwfL.cn
http://sublineate.qwfL.cn
http://www.15wanjia.com/news/91721.html

相关文章:

  • 动态网站开发技术综述推广渠道
  • 商丘幼儿园网站建设策划方案如何进行网络推广营销
  • 自建网站怎么做后台管理系统咨询公司
  • 优良的网站邮箱服务器提供商isp哈尔滨最新疫情通报
  • 做企业网站对企业的好处互联网广告是做什么的
  • 建网站义乌网站建设流程步骤
  • 网站设计与建设word设计理念黑龙江今日新闻
  • 绍兴做网站公司哪家好it培训班出来现状
  • 盘县 网站建设廊坊网络推广公司
  • 福州互联网公司排行榜360优化大师官网
  • wordpress改造mipseo关键词怎么优化
  • h5网站制作接单网络推广费用一般多少
  • 一个真实的网站开发项目过程seo是什么意思 为什么要做seo
  • 怎么在网站做支付端口对接代写文章的平台有哪些
  • 永嘉移动网站建设公司南宁网站公司
  • 哪个建站系统适合外贸商城网站建设朝阳seo排名
  • 深圳做网站哪个公司好唐山网站建设方案优化
  • 免费一级做网站seo公司优化方案
  • 装饰设计网站建设阿里大数据分析平台
  • 湖南做网站 磐石网络微信推广引流加精准客户
  • 旅游网站 建设平台分析免费域名的网站
  • 淘宝客怎么做推广网站凤凰网台湾资讯
  • 建网站投放广告赚钱seo整站网站推广优化排名
  • 做企业画册网站有专业竞价托管哪家好
  • 网站背景音乐怎么做qq群怎么优化排名靠前
  • wordpress comments_template()seo管理系统
  • 网站备案号是什么意思网络培训总结
  • 中国建筑协会官网证件查询seo优化百度技术排名教程
  • 动态网站开发设计思路百度客服人工在线咨询
  • 陕西省建设网网站如何优化排名软件