题目链接:

题意:
给出一个长度为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. C++:对象的初始化和构造函数

    对象的初始化和构造函数 构造函数:是一种特殊的成员函数,它主要用于为对象分配空间,进行初始化.构造函数 的名字必须与类名相同,它不要用户来调用,而是在建立对象时自动执行的 形式一: 类名 对象名(实参 ...

  2. java--面向接口编程

    之前看的一本书的笔记,上周再看设计模式的时候,想到了这篇之前在看某本书时候的笔记. 面向接口编程很重要的一点就是接口回调,用接口声明的变量称作接口变量,属于引用型变量,可以存放实现该接口的类的实例的引 ...

  3. jumplist和changlist

    用jumplist可以在不同的访问过的位置之间跳转 C-O到上一个 C-I到下一个位置 :jumps列出跳转列表 changlist列出最近的改动点 g;到上一个,g,到下一个 :changes列出相 ...

  4. PHP Redis 普通封装类

    class redisInit { private $redis; //redis对象 /** * 初始化Redis * $config = array( * 'server' => '127. ...

  5. 在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务(老罗学习笔记6)

    一:Eclipse下 1.创建工程: ---- 2.创建后目录 3.添加java函数 4.在src下创建package,在package下创建file 5.res---layout下创建xml文件,命 ...

  6. ubuntu10.04开启root登陆

    半年没有用ubuntu了,以前用的是8.10,现在装了一个10.04,第一印象就是登陆窗口变了,哎,比较喜欢用root用户登录系统,不喜欢非root用户,做任何事都要来一下sudo,10.04的登陆窗 ...

  7. git中手动删除的文件如何在git中删除

    在日常开发中,我们可能或手动删除(delete键删除的)一些文件,然而我们本来应该是用git rm fileName命令删除的,但是现在我们手动删除了,那么要如何在git里面讲那些手动删除的文件删除呢 ...

  8. java---面试题---.java"源文件中可以包括多个类(不是内部类)

    答题时,先答是什么,再答有什么作用和要注意什么 一个".java"源文件中可以有多个类,但只能有一个public的类,并且public的类名必须与文件名相一致,main方法只能写在 ...

  9. bzoj3931: [CQOI2015]网络吞吐量

    将最短路图找出来,跑maxflow即可.有注意到数据范围.然后输出的时候%dWA了三次QAQ... #include<cstdio> #include<cstring> #in ...

  10. bzoj3261: 最大异或和

    可持久化trie.又是%%%Xs酱... #include<cstdio> #include<cstring> #include<iostream> #includ ...