codeforces 701C C. They Are Everywhere(尺取法)
题目链接:
2 seconds
256 megabytes
standard input
standard output
Sergei B., the young coach of Pokemons, has found the big house which consists of n flats ordered in a row from left to right. It is possible to enter each flat from the street. It is possible to go out from each flat. Also, each flat is connected with the flat to the left and the flat to the right. Flat number 1 is only connected with the flat number 2 and the flat number n is only connected with the flat number n - 1.
There is exactly one Pokemon of some type in each of these flats. Sergei B. asked residents of the house to let him enter their flats in order to catch Pokemons. After consulting the residents of the house decided to let Sergei B. enter one flat from the street, visit several flats and then go out from some flat. But they won't let him visit the same flat more than once.
Sergei B. was very pleased, and now he wants to visit as few flats as possible in order to collect Pokemons of all types that appear in this house. Your task is to help him and determine this minimum number of flats he has to visit.
The first line contains the integer n (1 ≤ n ≤ 100 000) — the number of flats in the house.
The second line contains the row s with the length n, it consists of uppercase and lowercase letters of English alphabet, the i-th letter equals the type of Pokemon, which is in the flat number i.
Print the minimum number of flats which Sergei B. should visit in order to catch Pokemons of all types which there are in the house.
3
AaA
2
7
bcAAcbc
3
6
aaBCCe
5 题意: 问连续子序列包含所有类型字母的最短长度是多少; 思路: 尺取法,枚举左端点,移动右端点; AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e5+10;
const int maxn=500+10;
const double eps=1e-6; int n,num[60],vis[60],a[N];
char s[N]; int check(char x)
{
if(x>='a'&&x<='z')return x-'a';
else return x-'A'+26;
}
int main()
{
read(n);
scanf("%s",s+1);
int cnt=0;
For(i,1,n)
{
a[i]=check(s[i]);
//if(!vis[a[i]])cnt++;
vis[a[i]]=1;
}
For(i,0,59)if(vis[i])cnt++;
//cout<<cnt<<endl;
int ans=inf;
int l,r=1,sum=0;
For(i,1,n)
{
l=i;
while(1)
{
if(r>n)break;if(sum>=cnt)break;
num[a[r]]++;
if(num[a[r]]==1)sum++; r++;
}
//cout<<r<<" "<<sum<<endl;
if(sum>=cnt)ans=min(ans,r-l);
num[a[i]]--;
//cout<<num[a[i]]<<endl;
if(num[a[i]]==0)sum--;
}
cout<<ans<<"\n";
return 0;
}
codeforces 701C C. They Are Everywhere(尺取法)的更多相关文章
- CF 701C They Are Everywhere(尺取法)
题目链接: 传送门 They Are Everywhere time limit per test:2 second memory limit per test:256 megabytes D ...
- Codeforces 676C Vasya and String(尺取法)
题目大概说给一个由a和b组成的字符串,最多能改变其中的k个字符,问通过改变能得到的最长连续且相同的字符串是多长. 用尺取法,改变成a和改变成b分别做一次:双指针i和j,j不停++,然后如果遇到需要改变 ...
- [CF660C]Hard Process(尺取法)
题目链接:http://codeforces.com/problemset/problem/660/C 尺取法,每次遇到0的时候补一个1,直到补完或者越界为止.之后每次从左向右回收一个0点.记录路径用 ...
- CodeForces 701C They Are Everywhere 尺取法
简单的尺取法…… 先找到右边界 然后在已经有了所有字母后减小左边界…… 不断优化最短区间就好了~ #include<stdio.h> #include<string.h> #d ...
- Codeforces Educational Codeforces Round 5 D. Longest k-Good Segment 尺取法
D. Longest k-Good Segment 题目连接: http://www.codeforces.com/contest/616/problem/D Description The arra ...
- Codeforces Round #364 (Div.2) C:They Are Everywhere(双指针/尺取法)
题目链接: http://codeforces.com/contest/701/problem/C 题意: 给出一个长度为n的字符串,要我们找出最小的子字符串包含所有的不同字符. 分析: 1.尺取法, ...
- Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)
题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...
- codeforces 814 C. An impassioned circulation of affection 【尺取法 or DP】
//yy:因为这题多组数据,DP预处理存储状态比每次尺取快多了,但是我更喜欢这个尺取的思想. 题目链接:codeforces 814 C. An impassioned circulation of ...
- Codeforces Round #364 (Div. 2)C. They Are Everywhere(尺取法)
题目链接: C. They Are Everywhere time limit per test 2 seconds memory limit per test 256 megabytes input ...
随机推荐
- MongoDb 判断字段长度比较好的方法
查询某字段长度超过一定长度时的方法, MongoDB中可能不好处理,一般这样: db.test.find({ $where:"this.F_DAQDATA.legnth>600&quo ...
- Engine中如何进行七参数投影转换?
来自:http://zhihu.esrichina.com.cn/?/question/6858 解决办法]:首先创建自定义geotransformation,然后用IGeometry.Project ...
- android TextView 设置字体大小
package com.example.yanlei.yl4; import android.graphics.Color;import android.os.Bundle;import androi ...
- 【css】设置div位于浏览器的最底层,离用户最远
有时候切换发现某块div一直悬浮在最上层,怎么设置div位于浏览器的最底层.离用户最远? <style> .in{ z-index: -1; } </style> 然后引用in ...
- ubuntu配置django
安装 安装Apache sudo apt-get install apache2 安装Django 下载Django 安装mod_wsgi sudo apt-get install libapache ...
- 解决Gradle执行命令时报Could not determine the dependencies of task ':compileReleaseJava'.
Could not determine the dependencies of task ':compileReleaseJava'. > failed to find target andro ...
- servlet基础梳理(一)
将近一个月没看servlet了,再加上第一次学习也没有深入.仅仅是笼统的看了一遍,编了一点基础案例就过去了,如今再去看感觉跟没学过一样.这里再用一点时间把这些基础都梳理一下,加深印象并为以后高速复习做 ...
- libpcre.so.0 被删除怎么办?
删除 #rpm -e --nodeps pcre-6.6-1.1 1> wget http://mirror.centos.org/centos/6/os/i386/Packages/pc ...
- web前端面试系列 - 数据结构(两个栈模拟一个队列)
一. 用两个栈模拟一个队列 思路一: 1. 一个栈s1作为数据存储,另一个栈s2,作为临时数据存储. 2. 入队时将数据压人s1 3. 出队时将s1弹出,并压人s2,然后弹出s2中的顶部数据,最后再将 ...
- 优化梯度计算的改进的HS光流算法
前言 在经典HS光流算法中,图像中两点间的灰度变化被假定为线性的,但实际上灰度变化是非线性的.本文详细分析了灰度估计不准确造成的偏差并提出了一种改进HS光流算法,这种算法可以得到较好的计算结果,并能明 ...