尺取法 map

来源:互联网 发布:外汇 交易系统测试软件 编辑:程序博客网 时间:2024/05/01 07:26
C. They Are Everywhere
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.

Input

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.

Output

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.

Examples
input
3
AaA
output
2
input
7
bcAAcbc
output
3
input
6
aaBCCe
output
5
Note

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

Test: #1, time: 0 ms., memory: 2096 KB, exit code: 0, checker exit code: 0, verdict: OK
Checker Log
ok answer is '2'
Test: #2, time: 0 ms., memory: 2100 KB, exit code: 0, checker exit code: 0, verdict: OK
Checker Log
ok answer is '3'
Test: #3, time: 0 ms., memory: 2100 KB, exit code: 0, checker exit code: 0, verdict: OK
Checker Log
ok answer is '5'
Test: #4, time: 0 ms., memory: 2100 KB, exit code: 0, checker exit code: 0, verdict: OK
Checker Log
ok answer is '1'
Test: #5, time: 0 ms., memory: 2096 KB, exit code: 0, checker exit code: 0, verdict: OK
Checker Log
ok answer is '1'
Test: #6, time: 0 ms., memory: 2096 KB, exit code: 0, checker exit code: 0, verdict: OK
Checker Log
ok answer is '52'
Test: #7, time: 0 ms., memory: 2104 KB, exit code: 0, checker exit code: 0, verdict: OK
Checker Log
ok answer is '1'
Test: #8, time: 0 ms., memory: 2080 KB, exit code: 0, checker exit code: 0, verdict: OK
Checker Log
ok answer is '2'
Test: #9, time: 0 ms., memory: 2108 KB, exit code: 0, checker exit code: 0, verdict: OK
Checker Log
ok answer is '2'
Test: #10, time: 0 ms., memory: 2092 KB, exit code: 0, checker exit code: 0, verdict: OK
Checker Log
ok answer is '3'
Test: #11, time: 15 ms., memory: 2076 KB, exit code: 0, checker exit code: 0, verdict: OK
Checker Log
ok answer is '5268'

0 0
原创粉丝点击