【题目链接】:http://codeforces.com/contest/701/problem/C

【题意】



让你选择一段最短的区间;

使得这段区间里面包含所有种类的字符;

【题解】



之前都是用二分写;

现在会用类似队列的思路写了;

就是先确定左端点;

然后右端点右移;

直到出现所有种类;(这时候右端点就没必要再右移了)

然后右端点不动,右移左端点;

然后如果这时候又没有全部种类,就再右移右端点;

O(N)的复杂度吧



【Number Of WA】



0



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) cin >> x
#define pri(x) cout << x
#define ms(x,y) memset(x,y,sizeof x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5+100; map <char,int> dic;
int n,tot,ans=N;
char s[N]; int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false);
rei(n);
rei((s+1));
rep1(i,1,n)
if (!dic[s[i]])
{
dic[s[i]] = 1;
tot++;
}
dic.clear();
int l = 1,r = 1,now = 0;
dic[s[1]] = 1;
now = 1;
while (r<=n)
{
if (l<=r && now==tot)
{
ans = min(ans,r-l+1);
dic[s[l]]--;
if (dic[s[l]]==0) now--;
l++;
}
else
{
r++;
if (r<=n)
{
dic[s[r]]++;
if (dic[s[r]]==1) now++;
}
}
}
pri(ans<<endl);
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}

【codeforces 701C】They Are Everywhere的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  3. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  4. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  5. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  6. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  7. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  8. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

  9. 【codeforces 515D】Drazil and Tiles

    [题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...

随机推荐

  1. Git:代码冲突常见解决方法【转】

    本文转载自:http://blog.csdn.net/iefreer/article/details/7679631 如果系统中有一些配置文件在服务器上做了配置修改,然后后续开发又新添加一些配置项的时 ...

  2. Git新建本地分支与远程分支关联问题:git branch --set-upstream【转】

    本文转载自:http://blog.csdn.net/netwalk/article/details/21088405 Git新建本地分支与远程分支关联问题:git branch --set-upst ...

  3. E20171006-hm

    trace  vt. 跟踪,追踪; 追溯,探索; 探索; 查找;          vi. 沿着一小径或道路前进; 可以追溯的;            n. 痕迹; 痕迹,踪迹; 微量,极少量; [植 ...

  4. 关于swoole 和golang 的压力测试结果

    一.环境介绍 linux centos7 php7.1.18 go1.12.1 2核4G内存 二.代码 swoole代码 <?php $http = new swoole_http_server ...

  5. [ NOI 2005 ] 聪聪与可可

    \(\\\) \(Description\) 一张\(N\)个点,\(M\)条边的有向图中,猫在\(A\)点,鼠在\(B\)点,每一秒两者按照以下规则移动: 猫先走去往老鼠所在地的最短路,可以走一步或 ...

  6. [ HAOI 2008 ] 玩具取名

    \(\\\) \(Description\) 在一个只有\(W,I,N,G\)的字符集中,给出四个字符的若干映射,每个映射为一个字符映射到两个字符,现给你一个假定由一个字符经过多次映射产生的字符串,问 ...

  7. web流行工具

    中小型公司: Node.js:现代工业化前端的基础: RequireJS:AMD规范, 即将过时的 JavaScript 模块化方案: Bower:前端模块源: npm:前端工具源,另一个潜在的前端模 ...

  8. CSS——◇demo

    核心思想:嵌套盒子中的◇超过父盒子的部分隐藏. 第一种写法: <!DOCTYPE html> <html> <head> <meta charset=&quo ...

  9. c指针之内存释放

    // 1.正常使用包含指针的结构体 // 2.正常使用元素类型为指针的vector #include<string.h> #include<stdio.h> #include& ...

  10. json 新用

    如果使用struts2的action,可以省去属性赋值的工夫. 但是假如你没有使用struts2,而且使用的是ajax请求,通过json来传递参数.那我下面所说的对你可能是一个很好的解脱,从此告别re ...