【牛客161 - A】字符串(尺取法,桶标记法)

题干:

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

小N现在有一个字符串S。他把这这个字符串的所有子串都挑了出来。一个S的子串T是合法的,当且仅当T中包含了所有的小写字母。小N希望知道所有的合法的S的子串中,长度最短是多少。

输入描述:

一行一个字符串S。只包含小写字母。S的长度不超过106.

输出描述:

一行一个数字,代表最短长度。数据保证存在一个合法的S的子串。

示例1

输入

复制

ykjygvedtysvyymzfizzwkjamefxjnrnphqwnfhrnbhwjhqcgqnplodeestu

输出

复制

49

解题报告:

   尺取,或者更新最后一次出现的坐标也可以(我叫他桶标记法,类似的题目HDU 3333  SPOJ DQUERY)

错误代码:

#include<bits/stdc++.h>using namespace std;
char s[1000005];
int vis[30];
int main()
{scanf("%s",s);int len = strlen(s);memset(vis,0,sizeof vis);int l = 0,r = 0,cnt = 0,minn = 0x3f3f3f3f;while(r<len) {if(vis[s[r]-'a'] == 0) {cnt++;}vis[s[r]-'a']++;if(cnt == 26) break;r++;}while(r < len) {vis[s[r]-'a']++;while(vis[s[l] - 'a'] > 1) {vis[s[l] - 'a']--;l++;}minn = min(minn,r-l+1);r++;//先更新minn,再更新r}printf("%d\n",minn);return 0 ;}
// qwertyuioplkjhgfdsazxcvbnm

总结:

  刚开始自己写的这个代码有点想少了,本来是想都放在一个while中的,但是感觉这样逻辑结构不是很清晰,然后就想把它新开一个while放进去,但是在边界处理的时候出现了问题,那就是我没有我在cnt==26的时候break了,但是r++这步没有执行,然后进入第二个while,又vis++了一次,也就是说同一个s[r],我们在vis数组中计算了两次。

   而你不能直接把r++放到if(cnt == 26) break;前面,因为这样的话,出现的第二个问题就是,比如你输入aaaabcdefg....xyz,那么扫到最后一个字符r++,break。然后就进不去下面的while循环了、、因为这时的r==len,所以进不去这个循环,l就没法前移,就找不到最短的解,于是会wa。

AC代码1:(自己写 ,有点麻烦了)

#include<bits/stdc++.h>using namespace std;
char s[1000005];
int vis[30];
int main()
{scanf("%s",s);int len = strlen(s);memset(vis,0,sizeof vis);int l = 0,r = 0,cnt = 0,minn = 0x3f3f3f3f;while(r<len) {if(vis[s[r]-'a'] == 0) cnt++;vis[s[r]-'a']++;if(cnt == 26) break;r++;}while(vis[s[l] - 'a'] > 1) {vis[s[l] - 'a']--;l++;}minn = min(minn,r-l+1);r++;while(r < len) {vis[s[r]-'a']++;while(vis[s[l] - 'a'] > 1) {vis[s[l] - 'a']--;l++;}minn = min(minn,r-l+1);r++;//先更新minn,再更新r } printf("%d\n",minn);return 0 ;} 
// qwertyuioplkjhgfdsazxcvbnm

AC代码2:(在线更新坐标的思想)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <string>
#include <deque>
#include <set>
#include <queue>
using namespace std;
#define  ll long long 
#define  N 1000009
#define  gep(i,a,b)   for(int  i=a;i<=b;i++)
#define  gepp(i,a,b)  for(int  i=a;i>=b;i--)
#define  gep1(i,a,b)  for(ll i=a;i<=b;i++)
#define  gepp1(i,a,b) for(ll i=a;i>=b;i--)    
#define  mem(a,b)  memset(a,b,sizeof(a))
#define  P  pair<int,int>u+
char s[N];
int loc[30];
int  main()
{scanf("%s",s);int l=0;int len=strlen(s);int ans=len+1;mem(loc,-1);int cnt=0;//只要[l,i]区间里含有26个字母即可gep(i,0,len-1){if(loc[s[i]-'a']==-1){cnt++;}        loc[s[i]-'a']=i;//该字母最大的坐标while(l<loc[s[l]-'a'])  l++;//后面有了,那么前面的就可以不用了,l++。减小去区间长度if(cnt==26){ans=min(ans,i-l+1);}}printf("%d\n",ans);return 0;
}

 AC代码3:

#include <bits/stdc++.h>
#define N 1000010
#define INF 0x3f3f3f3f
using namespace std;int main()
{int i,l=0,ans=INF;int vis[200]={0};string s;set<char>myset;cin>>s;for(i=0;s[i];i++){myset.insert(s[i]);vis[s[i]]++;while(vis[s[l]]>1)vis[s[l]]--,l++;if(myset.size()==26)ans=min(ans,i-l+1);}cout<<ans<<endl;return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441675.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql innodb 全表锁,Mysql InnoDB行锁及表锁分享

一. 背景知识二. 步入正题&#xff1a;表锁和行锁1.1. 表锁 vs 行锁在 MySQL 中锁的种类有很多&#xff0c;但是最基本的还是表锁和行锁&#xff1a;表锁指的是对一整张表加锁&#xff0c;一般是 DDL 处理时使用&#xff0c;也可以自己在 SQL 中指定&#xff1b;而行锁指的是锁…

【牛客 - 185A】无序组数 (思维,数学,因子个数)

题干&#xff1a; 时间限制&#xff1a;C/C 1秒&#xff0c;其他语言2秒 空间限制&#xff1a;C/C 131072K&#xff0c;其他语言262144K 64bit IO Format: %lld 题目描述 给出一个二元组&#xff08;A,B&#xff09; 求出无序二元组&#xff08;a,b&#xff09; 使得&#x…

php万能查询用预,PHP 与 mysql

一、php 的 sql 注入攻击1.1、什么是 sql 注入攻击用户提交一段数据库查询代码&#xff0c;根据返回的结果&#xff0c;获得某些他想得到的数据。比如 &#xff1a;查询某个管理员是否存在&#xff0c;一般程序员会这么写$sql "select * from user where nameluluyii and…

php 判断radio选中哪个,jquery如何判断单选按钮radio是否选中

jquery判断单选按钮radio是否选中的方法&#xff1a;1、加载页面的时候获取id&#xff0c;代码为【var fs$("#"id).val()】&#xff1b;2、点击按钮的时候获取id&#xff0c;代码为【var id $(this).attr("id")】。本教程操作环境&#xff1a;windows7系统…

【qduoj】【超级楼梯进阶版】

题干&#xff1a; 描述 N级阶梯&#xff0c;人可以一步走一级&#xff0c;也可以一步走两级&#xff0c;求人从阶梯底端走到顶端可以有多少种不同的走法。 输入 一个整数n&#xff0c;代表台阶的阶数。 输出 求人从阶梯底端走到顶端可以有多少种不同的走法&#xff0c;输出结…

matlab在光学实验中的应用,matlab在光学实验中的应用

matlab在光学实验中的应用 《MATLAB》课程论文MATLAB 在光学实验中的应用姓名&#xff1a;学号&#xff1a;专业&#xff1a;班级&#xff1a;指导老师&#xff1a;学院&#xff1a;完成日期&#xff1a;1MATLAB 在波动光学中的应用(姓名&#xff1a;郑苗苗 12012241736 2012 级…

【HDU - 6016】Count the Sheep (思维,类似二分图)

题干&#xff1a; Altough Skipping the class is happy, the new term still can drive luras anxious which is of course because of the tests! Luras became worried as she wanted to skip the class, as well as to attend the BestCoder and also to prepare for test…

如何生成时间序列matlab,求助:在MATLAB里如何输入时间序列中的时间

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼function[logRS,logERS,V]RSana(x,n,method,q)%Syntax:[logRS,logERS,V]RSana(x,n,method,q)%____________________________________________%% Performs R/Sanalysis on a time series.%% logRS is thelog(R/S).% logERS is theEx…

【CF#192 A】Funky Numbers (二分,查找,二元组)

题干&#xff1a; As you very well know, this years funkiest numbers are so called triangular numbers (that is, integers that are representable as , where k is some positive integer), and the coolest numbers are those that are representable as a sum of two…

matlab考试试题,matlab-考试试题-

matlab-考试试题- MATLAB 考试试题 (1) 产生一个1x10的随机矩阵&#xff0c;大小位于( -5 5)&#xff0c;并且按照从大到小的顺序排列好&#xff01;(注&#xff1a;要程序和运行结果的截屏)答案&#xff1a;a10*rand(1,10)-5;bsort(a, descend )1.请产生一个100*5 的矩阵&…

【HDU - 1702 】ACboy needs your help again! (栈和队列,水题模拟)

题干&#xff1a; ACboy was kidnapped!! he miss his mother very much and is very scare now.You cant image how dark the room he was put into is, so poor :(. As a smart ACMer, you want to get ACboy out of the monsters labyrinth.But when you arrive at the g…

java中jframe不存在怎么办,java – 设置JFrame背景,为什么这不起作用?

打印加载图像的宽度(如果为-1)则图像未正确加载.img Toolkit.getDefaultToolkit().createImage("red.png");System.out.println(img.getWidth(null)); // check what it prints值得阅读Loading Images Using getResource上的Java Tutorial您可以根据图像位置尝试任何…

后盾网经典原创视频教程php,《后盾网经典原创视频教程:PHP》139集

目录0_1 后盾网_IIS环境下PHP开发环境安装0 后盾网_PHP集成环境安装视频教程1 PHP视频教程 PHP基础(一)2 PHP视频教程 PHP基础(二)3 PHP视频教程 PHP基础(三)4 PHP视频教程 数据类型(一)5 PHP视频教程 数据类型(二)6 PHP视频教程 数据类型(三)7 PHP视频教程 类型转换 外部变量8…

【HDU - 1022】Train Problem I (栈模拟,水题,思维)

题干&#xff1a; As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a proble…

【HDU - 1216 】Assistance Required (模拟,类似素数打表,不是素数问题!)

题干&#xff1a; After the 1997/1998 Southwestern European Regional Contest (which was held in Ulm) a large contest party took place. The organization team invented a special mode of choosing those participants that were to assist with washing the dirty d…

任意阶魔方阵matlab程序,【精品】任意阶魔方阵算法(c语言)

n阶幻方是由前n^2(n的2次方)个自然数组成的一个n阶方阵&#xff0c;其各行、各列及两条对角线所含的n个数的和相等。洛书就是最基本的33阶魔方阵&#xff0c;做出某种最恰当的决定&#xff0c;横竖都有3个格。 0的倒数 a&#xff0d;1可以对于 n 阶单位矩阵 e 以及同阶的方阵 a…

【HDU - 1302】The Snail (模拟,水题)

题干&#xff1a; A snail is at the bottom of a 6-foot well and wants to climb to the top. The snail can climb 3 feet while the sun is up, but slides down 1 foot at night while sleeping. The snail has a fatigue factor of 10%, which means that on each succe…

悟空php微信复制的东西在哪找,微信收藏的文件在哪?从哪里能看到?

现在的微信有很多的小功能&#xff0c;非常的方便实用&#xff0c;但是很多功能大家都不知道&#xff0c;今天&#xff0c;开淘网小编就来教教大家怎么使用微信的“我的收藏”功能。这个功能非常实用&#xff0c;而且收藏的源文件删除的话&#xff0c;我们从收藏里还是一样能用…

python批量打印机excel,python自动化办公系列03_单个以及批量处理excel文件

先贴上数据集&#xff0c;链接&#xff1a;https://pan.baidu.com/s/1ttv7NwbRmfVPcj2iBHTAfg提取码&#xff1a;zg5v下面是关于如何计算每个销售额以及总销售的代码。import osimport pandas as pdos.chdir("C:\\Users\\yuyuk\\data science\\data analysis and descript…

【OpenJ_Bailian - 2299 】Ultra-QuickSort (归并排序 或 离散化 + 树状数组)

题干&#xff1a; In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequ…