div.2/C. They Are Everywhere<two pointer>
来源:互联网 发布:网络常用英文缩写 编辑:程序博客网 时间:2024/06/15 20:28
题意:
给出包含n (3<=n<=100000)个字符的字符串,计算出包含所有类型字符的最小区间长度。
题解:
Two pointer.注意区间的处理。
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int maxn=100000+100;char s[maxn];int vis[1000+10];int l=0,r=0,num=0;int n,ty=0;int Find(char t,int pos)//向右查找所需要的字符{ for(;pos<n;pos++) { if(s[pos]==t) { vis[s[pos]]++; return pos; } vis[s[pos]]++; } return 0;}int main (){ scanf("%d",&n); scanf("%s",s); for(int i=0;i<n;i++)//计算字符的种类 { if(vis[s[i]]==0) ty++; vis[s[i]]++; } memset(vis, 0, sizeof(vis)); for(int i=0;i<n;i++)//寻找起始区间 { if(vis[s[i]]==0) num++; vis[s[i]]++; if(num==ty) { r=i; break; } } int ans=r+1; for(int i=0;i<n;i++) { if(vis[s[i]]>1) { vis[s[i]]--; } else { ans=min(ans,r-i+1); int p=Find(s[i],r+1); if(p==0) break; else { r=p; vis[s[i]]--; ans=min(ans,r-i); } } } printf("%d\n",ans); return 0;}
0 0
- div.2/C. They Are Everywhere<two pointer>
- Codeforces Round #364 (Div. 2) C. They Are Everywhere __ two pointers or binary search
- Codeforces Round #364 div.2 C. They Are Everywhere 【尺追法】
- Codeforces #364(Div.2)C.They Are Everywhere【思维】
- Codeforces Round #364 (Div. 2) C. They Are Everywhere(二分)
- CF #364 (Div. 2)(C. They Are Everywhere 尺取法)
- Codeforces Round #364 (Div. 2)C. They Are Everywhere
- Codeforces Round #364 (Div. 2) C. They Are Everywhere
- codeforces 701C. They Are Everywhere two pointers
- Codeforces Round #364 (Div. 2) C. They Are Everywhere (尺取法)
- Codeforces Round #364 (Div. 2) C They Are Everywhere(滑窗)
- Codeforces Round #364 (Div. 2) C. They Are Everywhere (窗口滑动)
- Codeforces Round #364 (Div. 2) C. They Are Everywhere(stl+乱搞)
- Codeforces Round #364 (Div. 2) C. They Are Everywhere (尺取法)
- Codeforces Round #364 (Div. 2) C. They Are Everywhere (尺取法)
- CodeForces 701C They Are Everywhere
- They Are Everywhere
- 【Codeforces Round 364 (Div 2)C】【前缀和 双指针】They Are Everywhere 含有所有字符的最短区间长度
- 44.字符串逆序输出
- 文章标题
- Python中pass的作用
- 扩展欧几里得求逆元
- 校内ACM上出现Presentation Error的一点认识
- div.2/C. They Are Everywhere<two pointer>
- 地图导航ios(url)
- React-Native 对index.android.js的修改reload之后不能显示在模拟器上
- NOIP 2011 - 普及组 瑞士轮 归并排序 重庆一中高2018级竞赛班第四次测试 2016.7.25 Problem 4
- 题目4 ASCII码排序
- maven+eclipse项目问题报错原因(Cannot change version of project facet Dynamic web module to 2.5)
- java事件处理机制(自定义事件)
- 头文件注意
- Discuz_X3.2_SC_UTF8---论坛迁移