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 ...
随机推荐
- Docker私有仓库3
http://www.cnblogs.com/womars/p/5906435.html(上篇地址) 三.测试上两篇 #测试一下 [root@lh- data]# pwd /opt/data [roo ...
- HashMap和HashSet的源代码分析
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16 static final float DEFAULT_LOAD_ ...
- ****ural 1141. RSA Attack(RSA加密,扩展欧几里得算法)
1141. RSA Attack Time limit: 1.0 secondMemory limit: 64 MB The RSA problem is the following: given a ...
- 重复T次的LIS的dp Codeforces Round #323 (Div. 2) D
http://codeforces.com/contest/583/problem/D 原题:You are given an array of positive integers a1, a2, . ...
- 判断pc浏览器和手机浏览器方法
一 //平台.设备和操作系统 var system = { win: false, mac: false, xll: f ...
- sql or 与and同时有时要注意
如果是一天sql语句中有or和and同时在 正确:where xxx=xx and (xxx=xx or xxx=xx) 错误:where xxx=xx and xxx=xx or xxx=xx (这 ...
- Ant 删除目录 vs 清空目录
Apache Ant 可以用下面的命令来删除目录 <delete dir="${lucene.dir}"/> 但是这会删除整个目录,而我现在只想清空目录内的所有文件和子 ...
- C#生成word
using Microsoft.Office.Interop.Word; using System; using System.Collections.Generic; using System.Co ...
- VoIP的话音质量测量方法
严重的呼叫质量和性能管理问题会影响VoIP (Voice over IP)系统的运作.网络管理员等人需要理解基本的呼叫质量测量技术才能很好地监测.管理和诊断在VoIP中出现的这些问题.本文介绍了常用的 ...
- 邮件发布google blogger 博客
<?php $to = "@gmail.com";$subject = "Test mail";$message = "Hello! This ...