描述
给你一个长度为n的数字串,数字串里会包含1-m这些数字。如果连续的一段数字子串包含了1-m这些数字,则称这个数字字串为NUM串。你的任务是求出长度最短的NUM串是什么,只需要输出这个长度即可。
输入
输入数据有多组
第一行给定n和m。 (1<=n,m<=100000)
第二行n个数,表示数字串,数字间用空格隔开。
输出
如果存在NUM串则输出最短NUM串长度,否则输出"NO"。
样例输入
5 3
1 2 2 3 1
样例输出
3
C++水过
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 # include<iostream>
2 using namespace std;
3 int main()
4 {
5 int n,m,i,ap,j,l;
6 int a[ 200001],b[ 200001];
7 cin>>n>>m;
8 for ( int i= 1;i<=n;i++) cin>>a[i];
9 ap= 1;
10 i= 1;
11 b[a[i]]= 1;
12 l= 2147483647;
13 for(j= 2;j<=n;j++)
14 {
15 if(b[a[j]]== 0)ap++;
16 b[a[j]]++;
17 while(b[a[i]]> 1)
18 {
19 b[a[i]]--;
20 i++;
21 }
22 if ((ap==m)&&(l>j-i+ 1))l=j-i+ 1;
23
24 }
25 if (l== 2147483647) cout<< " NO "<<endl;
26 else cout<<l<<endl;
27 return 0;
28 }
2 using namespace std;
3 int main()
4 {
5 int n,m,i,ap,j,l;
6 int a[ 200001],b[ 200001];
7 cin>>n>>m;
8 for ( int i= 1;i<=n;i++) cin>>a[i];
9 ap= 1;
10 i= 1;
11 b[a[i]]= 1;
12 l= 2147483647;
13 for(j= 2;j<=n;j++)
14 {
15 if(b[a[j]]== 0)ap++;
16 b[a[j]]++;
17 while(b[a[i]]> 1)
18 {
19 b[a[i]]--;
20 i++;
21 }
22 if ((ap==m)&&(l>j-i+ 1))l=j-i+ 1;
23
24 }
25 if (l== 2147483647) cout<< " NO "<<endl;
26 else cout<<l<<endl;
27 return 0;
28 }