未出現的子串(unapeared)

來源:互聯網
上載者:User

標籤:

未出現的子串(unapeared)題目描述

有一個長度為n的數字串,其中會出現數字1,2,3,…,q(5≤q≤9)。現在的問題是,需要求出一個長度最小的串(出現的數字也是1~q),使得該串不是這個數字串的子串。為了簡化問題,你只需要輸出這個串的長度即可。例如對於數字串:
S=1 3 5 2 4 1 3 5 2 2 2 2 3 4 1 5 3 2(q=5)
長度為1和2的數字子串全出現過,但是你找不出子串s’=4。因此答案是3。
說明:此題中的數字子串,數字並不一定連續出現在母數字串中。如我們定義1 3是串153的一個子串,但3 5不是1 5 3的子串。串1 5 3的所有子串為:1,5,3,1 5,5 3,1 3,1 5 3,共7個。

輸入

第1行兩個數,串長n和出現的數位個數q;
第2行有n個數,表示該數字串每一位的數字。

輸出

未出現的子串的最小長度。

範例輸入
18 51 3 5 2 4 1 3 5 2 2 2 2 3 4 1 5 3 2
範例輸出
3
提示

 

對於30%的資料:1≤n≤20,q=5;
對於100%的資料:1≤n≤100000,5≤q≤9。

分析:因為是第一個未出現的串的長度,所以前面的長度的子串都出現過,所以從前往後統計1,2,...,q出現了幾遍即可;

代碼:

#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <algorithm>#include <climits>#include <cstring>#include <string>#include <set>#include <map>#include <queue>#include <stack>#include <vector>#include <list>#include <ext/rope>#define rep(i,m,n) for(i=m;i<=n;i++)#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)#define vi vector<int>#define pii pair<int,int>#define mod 1000000007#define inf 0x3f3f3f3f#define pb push_back#define mp make_pair#define fi first#define se second#define ll long long#define pi acos(-1.0)const int maxn=1e5+10;const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};using namespace std;using namespace __gnu_cxx;ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}int n,m,a[maxn],cnt;set<int>p;int main(){    int i,j,k,t;    scanf("%d%d",&n,&m);    rep(i,0,n-1)scanf("%d",&a[i]);    cnt=1;    rep(i,0,n-1)    {        p.insert(a[i]);        if(p.size()==m)cnt++,p.clear();    }    printf("%d\n",cnt);    //system ("pause");    return 0;}

 

未出現的子串(unapeared)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.