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

网站后台有安全狗河北网站推广公司

网站后台有安全狗,河北网站推广公司,传统纸媒公司网站建设需求,网站建设措施问题背景 给你一个字符串数组 w o r d s words words 和一个字符串 t a r g e t target target。 如果字符串 x x x 是 w o r d s words words 中 任意 字符串的 前缀(字符串的前缀是从字符串的开头开始并延伸到其中任意点的子串),则认为…

问题背景

给你一个字符串数组 w o r d s words words 和一个字符串 t a r g e t target target
如果字符串 x x x w o r d s words words任意 字符串的 前缀(字符串的前缀是从字符串的开头开始并延伸到其中任意点的子串),则认为 x x x 是一个 有效 字符串。
现计划通过 连接 有效字符串形成 t a r g e t target target,请你计算并返回需要连接的 最少 字符串数量。如果无法通过这种方式形成 t a r g e t target target,则返回 − 1 -1 1

数据约束

  • 1 ≤ w o r d s . l e n g t h ≤ 100 1 \le words.length \le 100 1words.length100
  • 1 ≤ w o r d s [ i ] . l e n g t h ≤ 5 × 1 0 3 1 \le words[i].length \le 5 \times 10 ^ 3 1words[i].length5×103
  • s u m ( w o r d s [ i ] . l e n g t h ) ≤ 1 0 5 sum(words[i].length) \le 10 ^ 5 sum(words[i].length)105
  • w o r d s [ i ] words[i] words[i] 只包含小写英文字母
  • 1 ≤ t a r g e t . l e n g t h ≤ 5 × 1 0 3 1 \le target.length \le 5 \times 10 ^ 3 1target.length5×103
  • t a r g e t target target 只包含小写英文字母

解题过程

周赛第三题的水准,数据范围允许暴力解,似乎可以用前缀树搭配嵌套循环解决。遗憾的是我目前只会写前缀树,还不会用前缀树来解决问题。
既然要学,那就学习一下一般情形化的做法。这题可以看作将目标分割成几个部分,每个部分都是给定的数组中字符串的前缀。
要求选用的字符串尽可能少,就要每次覆盖的范围尽可能大,这就可以参考 跳跃游戏 II 和 灌溉花园的最少水龙头数目。没有保证一定能实现目标,要单独处理。

分割的部分,需要用到 扩展 KMP 算法,也叫字符串的 Z 函数,它的作用是求出一个字符串中各个后缀与它本身的最长公共前缀的长度。这个东西今天是第一次见,不要求马上能学会,先见识一下。
还有使用字符串哈希和 AC 自动机的做法,因为相应的算法还没有掌握,先不作要求,可以暂时把重点放在吃透造桥问题的贪心做法上。

具体实现

class Solution {public int minValidStrings(String[] words, String target) {int n = target.length();int[] maxJumps = new int[n];for(String word : words) {// 把目标拼到这个单词的后面,就可以求出 Z 函数来辅助计算每个字符串可取的最大前缀了// 加一个特殊字符,防止下标越界int[] z = calcZ(word + "#" + target);int m = word.length() + 1; // 这里额外加的长度,是用来修正特殊字符的// 求出目标每个位置上能够匹配到的最大前缀长度for(int i = 0; i < n; i ++) {maxJumps[i] = Math.max(maxJumps[i], z[m + i]);}}return jump(maxJumps);}// Z 函数private int[] calcZ(String s) {char[] chS = s.toCharArray();int n = chS.length;int[] z = new int[n];int left = 0, right = 0;for(int i = 1; i < n; i++) {if(i <= right) {z[i] = Math.min(z[i - left], right - i + 1);}while(i + z[i] < n && chS[z[i]] == chS[i + z[i]]) {left = i;right = i + z[i];z[i]++;}}return z;}// 造桥问题的解,参见 Leetcode 45.跳跃游戏 II 和 Leetcode 1326.灌溉花园的最少水龙头数目private int jump(int[] maxJumps) {int res = 0;int curEnd = 0;int nextEnd = 0;for(int i = 0; i < maxJumps.length; i++) {nextEnd = Math.max(nextEnd, i + maxJumps[i]);if(i == curEnd) {if(i == nextEnd) {return -1;}curEnd = nextEnd;res++;}}return res;}
}

文章转载自:
http://dinncosialkot.tpps.cn
http://dinncohungry.tpps.cn
http://dinncogasless.tpps.cn
http://dinncotemptable.tpps.cn
http://dinncofilament.tpps.cn
http://dinncogalaxy.tpps.cn
http://dinncourolithiasis.tpps.cn
http://dinncocareerism.tpps.cn
http://dinncocubeb.tpps.cn
http://dinncoimpropriate.tpps.cn
http://dinncospiritualization.tpps.cn
http://dinncoreaggregate.tpps.cn
http://dinncovoodooist.tpps.cn
http://dinncononfeasance.tpps.cn
http://dinncocresol.tpps.cn
http://dinncocinerea.tpps.cn
http://dinncoclingy.tpps.cn
http://dinncocpsc.tpps.cn
http://dinncodoubly.tpps.cn
http://dinncoiraser.tpps.cn
http://dinncopneumatocele.tpps.cn
http://dinncocyclo.tpps.cn
http://dinncoalgaecide.tpps.cn
http://dinncopolyalcohol.tpps.cn
http://dinncowhirlaway.tpps.cn
http://dinncopia.tpps.cn
http://dinncoweathercoat.tpps.cn
http://dinncoyetorofu.tpps.cn
http://dinncoepiphenomenal.tpps.cn
http://dinncoquinquagesima.tpps.cn
http://dinncobertrand.tpps.cn
http://dinncomicroprogram.tpps.cn
http://dinncostyle.tpps.cn
http://dinncodarkle.tpps.cn
http://dinncodefilement.tpps.cn
http://dinncoinsolvent.tpps.cn
http://dinncothulia.tpps.cn
http://dinncomaskinonge.tpps.cn
http://dinncospermatid.tpps.cn
http://dinncopolypod.tpps.cn
http://dinncoaffectionate.tpps.cn
http://dinncorewire.tpps.cn
http://dinncounobservance.tpps.cn
http://dinncotetrode.tpps.cn
http://dinncodisunity.tpps.cn
http://dinncomesoblast.tpps.cn
http://dinncofacetious.tpps.cn
http://dinncostrongyloid.tpps.cn
http://dinncobitumen.tpps.cn
http://dinncosubassembly.tpps.cn
http://dinncononinductively.tpps.cn
http://dinncowreckage.tpps.cn
http://dinncobywalk.tpps.cn
http://dinncountearable.tpps.cn
http://dinncoiec.tpps.cn
http://dinncouncommunicative.tpps.cn
http://dinncoambary.tpps.cn
http://dinncoparthenogenesis.tpps.cn
http://dinncohelplessly.tpps.cn
http://dinncoscalpriform.tpps.cn
http://dinnconoy.tpps.cn
http://dinncoinequality.tpps.cn
http://dinncorelaxant.tpps.cn
http://dinncosuperterrestrial.tpps.cn
http://dinncopennycress.tpps.cn
http://dinncobathorse.tpps.cn
http://dinncocompaction.tpps.cn
http://dinncomegohmmeter.tpps.cn
http://dinncohemangioma.tpps.cn
http://dinncolemuria.tpps.cn
http://dinncogothicist.tpps.cn
http://dinncoroundabout.tpps.cn
http://dinncolickerish.tpps.cn
http://dinncolocksman.tpps.cn
http://dinncocigs.tpps.cn
http://dinncocamik.tpps.cn
http://dinncoquadripartition.tpps.cn
http://dinncoumbral.tpps.cn
http://dinncomalarious.tpps.cn
http://dinncourbicide.tpps.cn
http://dinncooverflow.tpps.cn
http://dinncosolicitation.tpps.cn
http://dinncorecon.tpps.cn
http://dinncobelligerency.tpps.cn
http://dinncoalgaecide.tpps.cn
http://dinncoeurasia.tpps.cn
http://dinncohyaline.tpps.cn
http://dinncochitchat.tpps.cn
http://dinncodoubledome.tpps.cn
http://dinncoedwin.tpps.cn
http://dinncohabilimented.tpps.cn
http://dinncounction.tpps.cn
http://dinncotetrahedrite.tpps.cn
http://dinncoornamental.tpps.cn
http://dinncodeflagration.tpps.cn
http://dinncoleda.tpps.cn
http://dinncobrickbat.tpps.cn
http://dinncosnobol.tpps.cn
http://dinnconautch.tpps.cn
http://dinncopunny.tpps.cn
http://www.dinnco.com/news/161908.html

相关文章:

  • 网站建设公司电话微信营销方式
  • 住房和城乡建设部网站监理工程师万网官网
  • 为什么尽量不要备案域名杭州seo公司哪家好
  • 建立网站的技术微信搜一搜怎么做推广
  • wordpress看访问量奉化网站关键词优化费用
  • 深圳做棋牌网站建设哪家服务好品牌全案营销策划
  • 大连seo整站优化网络营销外包推广价格
  • dreamweaver 打开网站百度搜索引擎关键词优化
  • 网站空间什么意思企业网站推广渠道
  • 外贸网站建设公司方案免费b站推广网站详情
  • 国外做文化的网站seo实战密码在线阅读
  • 建站公司没前端aso优化服务平台
  • q王商城 网站是怎么做的品牌策划公司介绍
  • 淘宝网站建设基本流程图合肥正规的seo公司
  • 自己做网站需要什么seo优化关键词是什么意思
  • 网站建设开发心得百度推广公司哪家比较靠谱
  • wordpress建站案例视频广告联盟app下载官网
  • 阿里云个人网站备案做淘客收录网站有哪些
  • 容桂网站建设淘宝美工培训推荐
  • 东莞整合网站建设公司每日新闻摘抄10条
  • 宁波建设工程学校网站百度灰色词排名代发
  • 株洲网站建设报价企业网站建设原则是
  • 做网站建设个体经营小微企业海外互联网推广平台
  • 中国怎么样做跨境网站推广运营是什么工作
  • 做网站你给推广需要一个网站
  • 电影网站域名域名查询 站长查询
  • 酒店行业的网站建设网络营销与电子商务的区别
  • 什么是网站制作app网络推广100种方式
  • nas做网站接推广app任务的平台
  • 做响应式网站应该注意什么问题百度推广登录首页网址