swun Broken Keyboard

來源:互聯網
上載者:User

題目來源http://218.194.91.48/acmhome/problemdetail.do?&method=showdetail&id=1456

描述

    Bruce Force's keyboard is broken, only a few keys are still working. Bruce has figured out he can still type texts by switching the keyboard layout whenever
he needs to type a letter which is currently not mapped to any of the m working keys of the keyboard.

 

    You are given a text that Bruce wishes to type, and he asks you if you can tell him the maximum number of consecutive characters in the text which can be typed without having to switch the keyboard layout. For simplicity,
we assume that each key of the keyboard will be mapped to exactly one character, and it is not possible to type other characters by combination of different keys. This means that Bruce wants to know the length of the largest substring of the text which consists
of at most m different characters.

輸入

    The input contains several test cases, each test case consisting of two lines. The first line of each test case contains the number m (1
≤ m ≤ 128
), which specifies how many keys on the keyboard are still working. The second line of each test case contains the text which Bruce wants to type. You may assume that the length of this text does not exceed 1 million characters. Note that the
input may contain space characters, which should be handled like any other character.

 

    The last test case is followed by a line containing one zero.

輸出

    For each test case, print one line with the length of the largest substring of the text which consists of at most m different
characters.

範例輸入

5
This can't be solved by brute force.
1
Mississippi
0

範例輸出

7
2

提示

Hint: The largest substring for the first test case is "_by_bru", where _ stands for a space character.

//在這裡注釋一下此題的具體思路,並告誡下,做題不可只求速率,還要考慮怎麼做,方法是什麼。欲速則不達,這個還是需要時間去鍛煉的。。//這道題問的是,給你一字串,然後問你n個不同的字元連成連續的字串最多有多少個字元.. #include<cstdio>#include<cstring>#include<iostream>using namespace std; char a[1000005];int t1,t2,l,r,n,res,len;int b[500];int main(){    while(scanf("%d",&n) && n){        memset(b,0,sizeof(b));        getchar();        gets(a);        len=strlen(a);        t1=t2=res=0;        l=r=0;        while(l<=r && r<len){ //區間             while(t1<=n && r<len){ //求在滿足不同字元的個數<規定的數..                 if(b[a[r]]==0) { //字元若沒出現過,標記                     b[a[r]]=1; t1++;                    if(t1>n)     break;                }                else b[a[r]]++;                r++; t2++;//增加右邊界             }            if(t2>res) res=t2;//求最大值             if(r>=len) break;//跳出迴圈             while(1){       b[a[l]]--;                if(b[a[l]]==0) break;                l++; t2--;                    }            l++; t1--; r++;        }        printf("%d\n",res);    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.