现在的位置: 首页 > 综合 > 正文

swun Broken Keyboard

2014年02月18日 ⁄ 综合 ⁄ 共 2065字 ⁄ 字号 评论关闭

题目来源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;
}

抱歉!评论已关闭.