div.2/C. They Are Everywhere<two pointer>
题意:
给出包含n (3<=n<=100000)个字符的字符串,计算出包含所有类型字符的最小区间长度。
题解:
Two pointer.注意区间的处理。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=100000+100;
char s[maxn];
int vis[1000+10];
int l=0,r=0,num=0;
int n,ty=0;
int Find(char t,int pos)//向右查找所需要的字符
{
for(;pos<n;pos++)
{
if(s[pos]==t)
{
vis[s[pos]]++;
return pos;
}
vis[s[pos]]++;
}
return 0;
}
int main ()
{
scanf("%d",&n);
scanf("%s",s);
for(int i=0;i<n;i++)//计算字符的种类
{
if(vis[s[i]]==0)
ty++;
vis[s[i]]++;
}
memset(vis, 0, sizeof(vis));
for(int i=0;i<n;i++)//寻找起始区间
{
if(vis[s[i]]==0)
num++;
vis[s[i]]++;
if(num==ty)
{
r=i;
break;
}
}
int ans=r+1;
for(int i=0;i<n;i++)
{
if(vis[s[i]]>1)
{
vis[s[i]]--;
}
else
{
ans=min(ans,r-i+1);
int p=Find(s[i],r+1);
if(p==0)
break;
else
{
r=p;
vis[s[i]]--;
ans=min(ans,r-i);
}
}
}
printf("%d\n",ans);
return 0;
}
div.2/C. They Are Everywhere<two pointer>的更多相关文章
- div蒙版+可移动
<html> <head> <title></title> <script src="jquery-1.8.2.js&q ...
- css实现遮罩层,父div透明,子div不透明
使用元素的opacity 属性,设置遮罩层的效果, 主要 样式是:background-color: #ooo; opacity:0.3; <div style="width:500p ...
- ABP集合贴
thead>tr>th,.table>tbody>tr>th,.table>tfoot>tr>th,.table>thead>tr>t ...
- 解决IE浏览器文字上面放无内容的元素选不中的方法
<div class="md1"> <p><i>sdgsgereryeryery</i></p> <label c ...
- PHP 实现“贴吧神兽”验证码
最早看到 “贴吧神兽” 验证码是在百度贴吧,吧主防止挖坟贴,放出了究极神兽验证码 例如: 地址:http://tieba.baidu.com/p/3320323440 可以用 PHP + JavaSc ...
- HTML页面弹出自定义对话框带遮蔽罩(使用JavaScript)
转载:http://blog.sina.com.cn/s/blog_610f47c50100ohe4.html 原理其实很简单:首先绘制弹出的自定义对话框,将其使用display:none隐藏,因为设 ...
- Accordion - 手风琴
//手风琴效果 <div style="overflow:hidden;height:400px;width:948px;"> <div class=" ...
- tableindex
在写代码的时候,失焦的第一反应便是ng-blur,没想到在一个标签上其作用了,多加了几个标签没反应,于是发现了tableindex,写的代码列子如下,希望可以帮助你: <img src='{{a ...
- HTML基础(五)——-css样式表——样式属性——格式与布局
一.position:fixed 锁定位置(相对于浏览器的位置),例如有些网站的右下角的弹出窗口. 示例: 二.position:absolute 绝对位置: 1.外层没有position:a ...
随机推荐
- hdu_5919_Sequence II(主席树)
题目链接:hdu_5919_Sequence II 题意: 给你n个数,m个询问,每次问你一个区间中每一种数在区间中第一次出现的位置的中位数,强制在线. 题解: 一看就是主席树搞,不过这里要询问第一次 ...
- Udemy - Angular 2 - The Complete Guide 笔记
1. install > npm install -g angular-cli 2. create app > ng new first-app 3. build app > cd ...
- WCF、Web API、WCF REST、Web Service的区别
Difference between WCF and Web API and WCF REST and Web Service The .Net framework has a number of ...
- hdu 1407 测试你是否和LTC水平一样高
Description 大家提到LTC都佩服的不行,不过,如果竞赛只有这一个题目,我敢保证你和他绝对在一个水平线上! 你的任务是: 计算方程x^2+y^2+z^2= num的一个正整数解. Inpu ...
- 亲身体验:Vultr超高性价比VPS评测教程
最具性价比的vps是哪家?综合考虑vps稳定性.机房速度.vps硬件配置,美国linode一度是vps市场里的王牌:digitalocean vps迅速介入云主机市场,SSD固态硬盘性能秒杀竞争对手, ...
- windows 环境下搭建django 错误分析总结
最近对于python核心编程学习完后,想进一步学习django的web开发,考虑再三还是决定在本机(win7)上搭建环境. 刚接触难免会出现问题,最大的一个问题是安装完django的包后,在cmd命令 ...
- oc 是否允许远程通知
UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSett ...
- [css3动画]渐隐渐现
测试 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8& ...
- laravel常用拓展库
1.laravel-dompdf:pdf生成器 git地址:https://github.com/barryvdh/laravel-dompdf 2.
- POJ 2516 Minimum Cost
每个物品分开做最小费用最大流. #include<cstdio> #include<cstring> #include<cmath> #include<vec ...