题目链接:

题意:
给出一个长度为n的字符串,要我们找出最小的子字符串包含所有的不同字符。

分析:
1.尺取法,不懂得可以看今天开始学算法系列-尺取法
2.二分法,具体见代码
PS:因为讲得太少所以附上A和B的题解:
A:结构体存储,将所有数sort一下,一头一尾输出即可
B:开辆个数组记录当前行或列是否有车,row[],col[],设两个变量a,b记录行列,放入一个车时,如果已标记则不用减去,否则减去(a--或b--),输出a*b即可。

代码:

//尺取法
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std; char inp[];
int ch[]; int main()
{
int n,s,e,i,poke = ,curpoke = ,res = n;
scanf("%d",&n);getchar();scanf("%s",inp);
for(i = ; i < n; i++) ch[inp[i]]++; for(i = ; i < ; i++)if(ch[i])poke++;
for(i = ; i < ; i++)ch[i] = ;
s=e=;
while(s <= e && e <= n)
{
if(curpoke < poke)
{
if(ch[inp[e]] == ) curpoke++;
ch[inp[e]]++; e++;
}
else
{
if(ch[inp[s]] == ) curpoke--;
ch[inp[s]]--;
s++;
}
if(poke == curpoke) res = min(res, e - s);
}
printf("%d",res);
}
//二分求解
#include<iostream>
#include<cstring>
#include<cstdio>
#define ll __int64
#define PI acos(-1.0)
#define mod 1000000007
using namespace std;
int n;
map<char,int> mp;
map<char,int> gg;
char a[];
int zong;
bool check(int xx)
{
int zha=;
gg.clear();
for(int i=;i<=xx;i++)
{
if(gg[a[i]]==)
zha++;
gg[a[i]]++;
}
if(zha>=zong)
return true;
for(int i=xx+;i<=n;i++)
{
if(gg[a[i]]==)
zha++;
gg[a[i]]++;
gg[a[i-xx]]--;
if(gg[a[i-xx]]==)
zha--;
if(zha>=zong)
return true;
}
return false;
}
int main()
{
zong=;
scanf("%d",&n);
getchar();
mp.clear();
for(int i=;i<=n;i++)
{
scanf("%c",&a[i]);
if(mp[a[i]]==)
{
mp[a[i]]=;
zong++;
}
}
int l=zong,r=n,mid=;
while(l<r)
{
mid=(l+r)/;
if(check(mid))
r=mid;
else
l=mid+;
}
printf("%d\n",l);
return ;
}

Codeforces Round #364 (Div.2) C:They Are Everywhere(双指针/尺取法)的更多相关文章

  1. 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 ...

  2. Codeforces Round #364 (Div. 2)

    这场是午夜场,发现学长们都睡了,改主意不打了,第二天起来打的virtual contest. A题 http://codeforces.com/problemset/problem/701/A 巨水无 ...

  3. Codeforces Round #364 (Div.2) D:As Fast As Possible(模拟+推公式)

    题目链接:http://codeforces.com/contest/701/problem/D 题意: 给出n个学生和能载k个学生的车,速度分别为v1,v2,需要走一段旅程长为l,每个学生只能搭一次 ...

  4. 树形dp Codeforces Round #364 (Div. 1)B

    http://codeforces.com/problemset/problem/700/B 题目大意:给你一棵树,给你k个树上的点对.找到k/2个点对,使它在树上的距离最远.问,最大距离是多少? 思 ...

  5. Codeforces Round #364 (Div. 2) B. Cells Not Under Attack

    B. Cells Not Under Attack time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  6. Codeforces Round #364 (Div. 2) Cells Not Under Attack

    Cells Not Under Attack 题意: 给出n*n的地图,有给你m个坐标,是棋子,一个棋子可以把一行一列都攻击到,在根据下面的图,就可以看出让你求阴影(即没有被攻击)的方块个数 题解: ...

  7. Codeforces Round #364 (Div. 2) Cards

    Cards 题意: 给你n个牌,n是偶数,要你把这些牌分给n/2个人,并且让每个人的牌加起来相等. 题解: 这题我做的时候,最先想到的是模拟,之后码了一会,发现有些麻烦,就想别的方法.之后发现只要把它 ...

  8. Codeforces Round #364 (Div. 2)->A. Cards

    A. Cards time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  9. Codeforces Round #364 (Div. 2) E. Connecting Universities

    E. Connecting Universities time limit per test 3 seconds memory limit per test 256 megabytes input s ...

随机推荐

  1. socklen_t在windows和linux平台下的头文件定义

    windows平台下:头文件:#include<ws2tcpip.h> linux平台下:下面两个头文件都有定义:1)#include <sys/socket.h>2)#inc ...

  2. 使用 powershell 的 grep 过滤文本

    使用 powershell 的 grep 过滤文本 有个log文件,大小在4M左右,要求找出里面耗时超过100s 的记录.首先想到了强大的 grep ,那么就搞起. 先在网上找一下资料,这篇文章,有几 ...

  3. iOS方法封装

    (void) setSubView:(UIView *)masterView subCCGRect:(CGRect)subCCGRect imageName:(NSString *)imageName ...

  4. 转:Cache相关

    声明:本文截取自http://blog.163.com/ac_victory/blog/static/1033187262010325113928577/ (1)“Cache”是什么 Cache(即高 ...

  5. mysql定时计划任务,ON COMPLETION [NOT] PRESERVE 当单次计划任务执行完毕后或当重复性的计划任务执行到了ENDS阶段。而声明PRESERVE的作用是使事件在执行完毕后不会被Drop掉

    当为on completion preserve 的时候,当event到期了,event会被disable,但是该event还是会存在当为on completion not preserve的时候,当 ...

  6. 1888. Pilot Work Experience(dfs+bfs)

    1888 dfs找出连通块 块内构造数据 bfs找出最值 如果有多个连通块 那max就为49 可以起点不同 这样记得修改后面的数据 写的老长了.. #include <iostream> ...

  7. SPRING STS Virgo OSGI 开发一 : bundle 项目的创建

    1. Spring STS 下载地址  (spring 最近改了站点 暂时不是太熟悉)     http://spring.io/tools/sts/all 2. 下载 Virgo 插件    htt ...

  8. JAVA将Excel中的报表导出为图片格式(三)换一种实现

    上一篇介绍了使用Java的Robot机器人实现截图,然后将剪贴板上的数据流生成PNG图片 但是经过博主的不断测试,在完全依赖远程桌面的没有终端显示器的服务器上 使用截图方式是不可行的,因为一旦使用了远 ...

  9. org.tigris.subversion.javahl.ClientException: Attempted to lock an already-locked dir异常解决方法

    myeclipse用svn提交的时候报错: Attempted to lock an already-locked dir svn: Working copy 'D:/Program Files/My ...

  10. 转:MVC3系列:~Html.BeginForm与Ajax.BeginForm

    Html.BeginForm与Ajax.BeginForm都是MVC架构中的表单元素,它们从字面上可以看到区别,即Html.BeginForm是普通的表单提交,而Ajax.BeginForm是支持异步 ...