【打CF,学算法——三星级】CodeForces 701C They Are Everywhere
来源:互联网 发布:kord机枪 知乎 编辑:程序博客网 时间:2024/06/05 08:27
【CF简介】
题目链接:CF 701C
题面:
Sergei B., the young coach of Pokemons, has found the big house which consists ofn 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 number2 and the flat number n is only connected with the flat numbern - 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 lengthn, it consists of uppercase and lowercase letters of English alphabet, thei-th letter equals the type of Pokemon, which is in the flat numberi.
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.
3AaA
2
7bcAAcbc
3
6aaBCCe
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.
题意:
给定一串仅由大小写字母组成的字符串,问其最短的连续子串可以包含母串所有元素的串长。
解题:
采用的是O(nlogn*52)的复杂度方法,计算前缀和,枚举左端点,二分右端点,每次比较是否包含了所有元素,进行区间移动,求最优值。虽然只用了139ms,但更快的方法是31ms。这题和POJ 3320是一样的,但POJ那题,n更大,为1000000,二分的复杂度是O(n(logn)^2)是过不了的,正确的解法是O(nlogn),左端点先设为1,先找到一个区间包含全部元素,随后不断调整左右端点,寻找最小长度(过程中用map维护),详解可看:POJ 3320 详解。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <map>#define LL long longusing namespace std;int cnt[100010][52];bool vis[52];char s[100010];int main(){ int n,len,am=0,tmp,le,ri,tcnt,ans=1000000;scanf("%d",&n);scanf("%s",s); len=strlen(s);if(s[0]<='Z')tmp=s[0]-'A';elsetmp=s[0]-'a'+26;cnt[1][tmp]++;vis[tmp]=1;for(int i=1;i<len;i++){ for(int j=0;j<52;j++) cnt[i+1][j]=cnt[i][j]; if(s[i]<='Z') tmp=s[i]-'A'; else tmp=s[i]-'a'+26; cnt[i+1][tmp]++; vis[tmp]=1;}for(int i=0;i<52;i++) if(vis[i]) am++;for(int i=0;i<n;i++){le=i;ri=n-1;bool flag=0;while(le<=ri){tcnt=0;int mid=(le+ri)>>1; for(int j=0;j<52;j++){if(vis[j]){tmp=cnt[mid+1][j]-cnt[i][j];if(tmp)tcnt++;}}if(tcnt==am){tmp=mid-i+1;flag=1;if(tmp<ans)ans=tmp;ri=mid-1;}else le=mid+1;}if(!flag)break;}printf("%d\n",ans);return 0;}
- 【打CF,学算法——三星级】CodeForces 701C They Are Everywhere
- 【打CF,学算法——三星级】Codeforces Round #313 (Div. 2) C. Gerald's Hexagon
- 【打CF,学算法——三星级】Codeforces 9C Hexadecimal's Numbers (解法汇总)
- 【打CF,学算法——三星级】CodeForces 689C Mike and Chocolate Thieves (二分)
- 【打CF,学算法——三星级】CodeForces 645C Enduring Exodus (二分+贪心)
- 【打CF,学算法——三星级】CodeForces 701B Cells Not Under Attack (分析)
- 【打CF,学算法——三星级】CodeForces 513C Second price auction (进制状态表示)
- CodeForces 701C They Are Everywhere
- 【打CF,学算法——三星级】CodeForces 615B Longtail Hedgehog (DFS/拓扑排序)
- 【打CF,学算法——三星级】CodeForces 567D One-Dimensional Battle Ships (二分)
- 【打CF,学算法——三星级】CodeForces 550D Regular Bridge (构造)
- 【打CF,学算法——三星级】CodeForces 216B Forming Teams (图论)
- 【打CF,学算法——三星级】Codeforces 698A Vacations (动态规划)
- 【打CF,学算法——三星级】Codeforces 704A Thor (模拟)
- [尺取法] cf 701c They Are Everywhere
- 【打CF,学算法——三星级】CodeForces 689B Mike and Shortcuts (最短路+spfa)
- 【打CF,学算法——三星级】CF Gym 100548K Last Denfence
- codeforces 701C They Are Everywhere(尺取法)
- android仿腾讯小火箭案例
- LeetCode#19 Remove Nth Node From End of List
- android面试专题(6)
- POJ 3641 Pseudoprime numbers(费马小定理,快速幂,数论)
- 使用SurfaceBiew录制视频
- 【打CF,学算法——三星级】CodeForces 701C They Are Everywhere
- Poj 3613 Cow Relays【Floyd+快速幂】
- 2016夏季练习——Prim
- 第一章 银联8583报文解析
- scala 学习笔记4 cache 和persist的区别
- EditText 对手机号码的处理
- AndroidStudio导入项目一直卡在Building gradle project info最快速解决方案
- 【poj1995】Raising Modulo Numbers
- QUSTOJ1782 A奶牛的锻炼(线性动归)