尺取法 map
来源:互联网 发布:外汇 交易系统测试软件 编辑:程序博客网 时间:2024/05/01 07:26
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
In the first test Sergei B. can begin, for example, from the flat number 1 and end in the flat number 2.
In the second test Sergei B. can begin, for example, from the flat number 4 and end in the flat number 6.
In the third test Sergei B. must begin from the flat number 2 and end in the flat number 6.
By fanyuheng, contest: Codeforces Round #364 (Div. 2), problem: (C) They Are Everywhere, Accepted, #
#include "stdio.h"#include "vector"#include"map"#include"iostream"using namespace std;const int maxn=100050;char s[maxn];map<char,int>Map;vector<char>Save;bool check() //当所有元素都遍历到了{for(int i=0;i<Save.size();i++)if(Map[Save[i]]==0)return false;return true;}int main(){int n;scanf("%d",&n);scanf("%s",s);for(int i=0;i<n;i++){if(Map[s[i]]==0){Map[s[i]]++;Save.push_back(s[i]);}//cout<<"Map"<<"[s["<<i<<"]]:"<<Map[s[i]]<<endl;}/*cout<<"Save:"<<endl;for(int i=0;i<Save.size();i++)cout<<Save[i];cout<<endl;*/Map.clear();int l,r,ans;ans=n;l=r=0;//cout<<"n:"<<n<<endl;while(l<n&&r<n){int step=0;do//当没有遍历完并且r指针没有出界 更新计数器然后R向右移动一格{Map[s[r++]]++;//cout<<"Map"<<"[s["<<r-1<<"]]R:"<<Map[s[r-1]]<<endl;}while(!check()&&r<n);do //当遍历完所有元素并且l指针没有出界 L指针可以向右移动一格{Map[s[l++]]--;//cout<<"Map"<<"[s["<<l-1<<"]]L:"<<Map[s[l-1]]<<endl;step++;}while(check()&&l<n);if(l) Map[s[--l]]++;//当step不为零时说明移动过了,回退一格,然后加一ans=min(ans,r-l);}cout<<ans<<endl;return 0;}
→Judgement Protocol
By fanyuheng, contest: Codeforces Round #364 (Div. 2), problem: (C) They Are Everywhere, Accepted, #
#include "stdio.h"#include "vector"using namespace std;int n,ans=1e9,t[256];bool check[256];char d[100010];vector<char> v;bool ok();int main(){ scanf("%d",&n); scanf("%s",d); for(int i=0;i<n;i++) if(!check[d[i]]) check[d[i]]=true,v.push_back(d[i]); int p=0,q=0; while(q<n){ do t[d[q++]]++;//先计数然后向右移动,更新计数器 while(!ok()&&q<n); while(ok()&&p<n) t[d[p++]]--; //模拟向左移动一个,更新计数器 if(p>0) t[d[--p]]++; //当终止循环的时候是超前一位的。 ans=min(ans,q-p); } printf("%d",ans);}bool ok(){ //判断当前字符串 是否所有都被访问了。 for(int i=0;i<v.size();i++) if(!t[v[i]]) return false; return true;}
→Judgement Protocol
3AaA
2
2
ok answer is '2'
7bcAAcbc
3
3
ok answer is '3'
6aaBCCe
5
5
ok answer is '5'
1A
1
1
ok answer is '1'
1g
1
1
ok answer is '1'
52abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
52
52
ok answer is '52'
2AA
1
1
ok answer is '1'
4qqqE
2
2
ok answer is '2'
10rrrrroooro
2
2
ok answer is '2'
15OCOCCCCiCOCCCOi
3
3
ok answer is '3'
100000zluLZdlVUzUarrVdiUrquZqVWcsLuzKUdgprhUutruasCtibsWCtgHmUaZVgduXZrntrUjlatViZsZsWeOigZVsXUOaauiguOmGWuipaqzuirlydVtrtgGzliqgUgZplssTczlrTseTeggasQjVaiUdaLZKVilsrjZHdWuUeGnsQbrdVguzUgyQjZzOZUUzrVuUiqthKvCQijKVlUdZpaUlLaUlzsauOsgQupuGlzpZqZyplaVzlacaiQUrOZliigPzigghrXZOdaiQautZaOtgpbaVHlarbLUuialQuglVlBtsVUzQjUqazVeuVglqsKsVjLlrzgqzWrUgOzlgVQaWgZrdaadZpiVVgibVtzziilgujLztVsaVWdrOlzuzrrbbQlurUzujLtvulQZKdasVeglsrOjVusjlVHisiUrLulzqtGzirCNijljzVztbziUVajVGqZzrGaOzzyrVsurJagbUuzOzCWlggsnHgzjzZbpGZgjWqqqU...
5268
5268
ok answer is '5268'
- 尺取法 map
- POJ 3320 (尺取法 map)
- POJ 3320 尺取法 + map + set
- poj 3320 尺取法 + map + set
- poj 3320 Jessica's Reading Problem(尺取法+map/hash)
- POJ 3320 Jessica's Reading Problem 尺取法 map运用
- 尺取法1——加set,map
- poj 3320 Jessica's Reading Problem(尺取法+map)
- 尺取法
- 尺取法
- 尺取法
- 尺取法
- 尺取法
- 尺取法
- 尺取法
- 尺取法
- 【尺取法】
- 尺取法
- android 判断手机号是否格式正确
- oracle安装错误:Could not execute auto check for display colors using command /usr/bin/xdpyinfo.
- string对象不可变的原理
- undefined reference to `s3c_dma_get_ops'
- FileWriter与BufferedWriter的区别
- 尺取法 map
- 微信支付问题总结
- 开关机命令详解
- ExecutorService(线程池)+线程
- Android 进程保活招式大全
- springMVC+mybaties+maven整合quertz
- 使用eclipse对android代码进行混淆
- 往github的readme里上传图片
- java多线程—Runnable、Thread、Callable区别