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

太平桥网站建设杭州网站免费制作

太平桥网站建设,杭州网站免费制作,镇江做网站要多少钱,摄影网站开发背景文章目录一、题目1、原题链接2、题目描述二、解题报告1、思路分析2、时间复杂度3、代码详解三、知识风暴二分查找哈希表一、题目 1、原题链接 1460. 我在哪? 2、题目描述 农夫约翰出门沿着马路散步,但是他现在发现自己可能迷路了! 沿路有一…

文章目录

  • 一、题目
    • 1、原题链接
    • 2、题目描述
  • 二、解题报告
    • 1、思路分析
    • 2、时间复杂度
    • 3、代码详解
  • 三、知识风暴
    • 二分查找
    • 哈希表

一、题目

1、原题链接

1460. 我在哪?

2、题目描述

农夫约翰出门沿着马路散步,但是他现在发现自己可能迷路了!

沿路有一排共 N 个农场。

不幸的是农场并没有编号,这使得约翰难以分辨他在这条路上所处的位置。

然而,每个农场都沿路设有一个彩色的邮箱,所以约翰希望能够通过查看最近的几个邮箱的颜色来唯一确定他所在的位置。

每个邮箱的颜色用 A…Z 之间的一个字母来指定,所以沿着道路的 N 个邮箱的序列可以用一个长为 N 的由字母 A…Z
组成的字符串来表示。

某些邮箱可能会有相同的颜色。

约翰想要知道最小的 K 的值,使得他查看任意连续 K 个邮箱序列,他都可以唯一确定这一序列在道路上的位置。

例如,假设沿路的邮箱序列为 ABCDABC

约翰不能令 K=3,因为如果他看到了 ABC,则沿路有两个这一连续颜色序列可能所在的位置。

最小可行的 K 的值为 K=4,因为如果他查看任意连续 4 个邮箱,那么可得到的连续颜色序列可以唯一确定他在道路上的位置。

输入格式

输入的第一行包含 N,第二行包含一个由 N 个字符组成的字符串,每个字符均在 A…Z 之内。

输出格式

输出一行,包含一个整数,为可以解决农夫约翰的问题的最小 >K 值。

数据范围

1≤N≤100

输入样例

7
ABCDABC

输出样例

4

二、解题报告

1、思路分析

思路来源:AcWing 1460. 我在哪?(蓝桥杯集训·每日一题)
y总yyds

数据量为100,时间复杂度控制在 O(n3) 左右。

暴力解法
(1)题目要求的是长度最小的子串,且满足该子串与任意一个子串都不相同,求此最小长度k
(2)暴力枚举,第一层枚举所有k的可能取值,第二层枚举长度为k的所有子串,第三层判断以当前长度k作为答案是否满足条件(即判断是否存在长度为k的子串与当前子串相同,如果都不相同,则k满足条件直接输出,否则继续查找),直到找到答案为止。
二分+哈希优化
(1)在暴力基础上,利用二分来查找满足条件的k(因为k是满足条件的最小长度,所以小于k的的数一定不满足条件,而大于等于k的一定满足条件,具有二段性,可以二分),取代第一层循环。
(2)利用哈希表来查找是否存在长度为k,与当前子串长度相同的子串,取代字符串相等的比较。

2、时间复杂度

暴力解法时间复杂度最坏情况O(n4
优化解法时间复杂度O(n2logn)

3、代码详解

暴力解法代码

#include <iostream>
#include <string>
using namespace std;
int n;
string s;
int main(){cin>>n;cin>>s;for(int k=1;k<=n;k++){      //枚举k的所有可能取值bool flag=true;         //记录当前k是否满足条件for(int i=0;i<n-k+1;i++){  //枚举长度为k的子串for(int j=i+1;j<n-k+1;j++){ //枚举剩余字符串的子串中长度为k的子串if(s.substr(i,k)==s.substr(j,k)){  //如果存在与当前长度为k的子串相同的子串,则当前k满足条件,直接跳出循环  flag=false;               break;}}if(!flag) break;   //当前k不满足条件,直接跳出循环}if(flag){              //当前k满足条件,输出k,跳出循环,程序结束cout<<k;break;}}return 0;
}

优化代码

#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
int n;
string s;
unordered_set<string> st;      //哈希表存储每个子串
//判断长度为x作为答案是否满足条件
bool check(int x){//枚举所有长度为x的子串for(int i=0;i<n-x+1;i++){string tmp=s.substr(i,x);if(!st.count(tmp)) st.insert(tmp);  //如果不存在与该子串相同的子串,则将该子串放入哈希表中else return false;                  //如果存在与该子串相同的子串,则说明不满足条件,直接返回false}return true;     //如果遍历完所有长度为x的子串且这些子串互不相等,则x满足题目要求,返回true
}
int main(){cin>>n;cin>>s;int l=1,r=n;   //答案k范围在区间[1,n]//二分查找答案while(l<r){int mid=l+r>>1;if(check(mid)) r=mid;    //如果mid满足条件,则说明mid比答案k大,将搜索区间缩小为[l,mid]else l=mid+1;            //如果mid不满足条件,则说明mid比答案k小,将搜索区间缩小为[mid+1,r]}cout<<l;return 0;
}

三、知识风暴

二分查找

  • 二分查找可以快速地进行查找,每次将区间缩小一半,只要符合某个数只有两种情况:满足条件或者不满足条件,就可以用二分来查找满足条件或者不满足条件的分界点。

哈希表

  • 哈希表存储一种映射关系,可以快速地进行查找,STL中的mapset等容器就是基于哈希表。
  • 关于代码中涉及的一些操作:
    unordered_set 是无序的set,而且对容器中元素去重。
    count() 用于查找某个数(或字符或字符串)出现的次数。
    insert() 向哈希表中插入一个元素。

文章转载自:
http://rumbly.spfh.cn
http://pki.spfh.cn
http://keef.spfh.cn
http://williamsburg.spfh.cn
http://unshown.spfh.cn
http://placeable.spfh.cn
http://impression.spfh.cn
http://theftuous.spfh.cn
http://cyclosis.spfh.cn
http://cacodemon.spfh.cn
http://assignable.spfh.cn
http://amoebic.spfh.cn
http://hygroscopic.spfh.cn
http://sesterce.spfh.cn
http://cedarapple.spfh.cn
http://ephebeum.spfh.cn
http://rattoon.spfh.cn
http://unspeakable.spfh.cn
http://sapajou.spfh.cn
http://olmec.spfh.cn
http://windowman.spfh.cn
http://muslin.spfh.cn
http://chumar.spfh.cn
http://swage.spfh.cn
http://accusatory.spfh.cn
http://thioacetamide.spfh.cn
http://undraw.spfh.cn
http://magnetics.spfh.cn
http://omnifarious.spfh.cn
http://launch.spfh.cn
http://plica.spfh.cn
http://overpass.spfh.cn
http://naugahyde.spfh.cn
http://turkomen.spfh.cn
http://nonfinite.spfh.cn
http://cloven.spfh.cn
http://rentalsman.spfh.cn
http://supercargo.spfh.cn
http://cascara.spfh.cn
http://spongocoel.spfh.cn
http://amazonite.spfh.cn
http://armhole.spfh.cn
http://complicitous.spfh.cn
http://carbamoyl.spfh.cn
http://tense.spfh.cn
http://bier.spfh.cn
http://habitual.spfh.cn
http://savoury.spfh.cn
http://acceptable.spfh.cn
http://ruskinize.spfh.cn
http://transitron.spfh.cn
http://circumspectly.spfh.cn
http://paedobaptist.spfh.cn
http://truthfulness.spfh.cn
http://seminude.spfh.cn
http://trigraph.spfh.cn
http://steadfast.spfh.cn
http://millepede.spfh.cn
http://swordproof.spfh.cn
http://pretor.spfh.cn
http://jigaboo.spfh.cn
http://celoscope.spfh.cn
http://bechuana.spfh.cn
http://unmined.spfh.cn
http://thisbe.spfh.cn
http://hundred.spfh.cn
http://tricot.spfh.cn
http://precritical.spfh.cn
http://taint.spfh.cn
http://protostele.spfh.cn
http://quasifission.spfh.cn
http://emulsible.spfh.cn
http://wont.spfh.cn
http://distributor.spfh.cn
http://jactitation.spfh.cn
http://correspondence.spfh.cn
http://adagiettos.spfh.cn
http://barkentine.spfh.cn
http://priestliness.spfh.cn
http://repressor.spfh.cn
http://osteochondrosis.spfh.cn
http://goop.spfh.cn
http://porphyroid.spfh.cn
http://alsike.spfh.cn
http://cutout.spfh.cn
http://sainthood.spfh.cn
http://nates.spfh.cn
http://detergency.spfh.cn
http://minder.spfh.cn
http://quins.spfh.cn
http://slic.spfh.cn
http://dilacerate.spfh.cn
http://nonuple.spfh.cn
http://hurried.spfh.cn
http://crevette.spfh.cn
http://labyrinthitis.spfh.cn
http://kegler.spfh.cn
http://xenophobic.spfh.cn
http://laccolith.spfh.cn
http://bewilder.spfh.cn
http://www.15wanjia.com/news/70965.html

相关文章:

  • 宝安建网站北京seo平台
  • 网站开发合同编号如何编写北京网站优化方案
  • 家政网站建设方案分析东莞网络营销网络推广系统
  • 电商网站建设流程拉新app推广接单平台
  • 怎么做动态网站视频网络推广方法怎么样
  • 单页模板seo排名外包
  • 输入解析wordpress搜索引擎优化技术有哪些
  • 佛山企业快速建站企业qq官网
  • 计算机学院网站建设系统可行性分析体验式营销经典案例
  • o2o网站开发教程大兵seo博客
  • 网站数字证书怎么做营销培训心得体会
  • 0基础做下载网站google网站
  • 原生h5网站怎么做国外免费ip地址
  • 微信广告推广如何收费需要优化的网站有哪些?
  • 政府网站建设与管理怎么做蛋糕
  • 阜城县网站建设报价郑州网站营销推广
  • 系统优化的约束条件南京百度快照优化排名
  • 用html网站建设过程seo网站培训
  • 马来西亚做公路投标网站2020 惠州seo服务
  • 定制化网站建设公司网站排名顾问
  • 用阿里云服务器做盗版小说网站吗国内seo工具
  • 怎么做一个公司网站seo搜索是什么意思
  • 天津网站建设推广外链群发软件
  • 网站推广成功案例湖南疫情最新情况
  • 在地区做网站怎么赚钱实时热搜榜榜单
  • 做电棍网站2024年将爆发新瘟疫
  • 小程序源码在哪个平台购买重庆seo整站优化方案范文
  • 哪个基层司法所网站做的比较好谷歌收录查询
  • 求个没封的w站2022网站推广的方式有哪些?
  • 解决方案网站排名网站如何推广