hdu2369 Broken Keyboard(类似dfs)

转载请注明出处:http://blog.csdn.net/u012860063

题目链接:http://acm.hdu.edu.cn/showproblem.php?

pid=2369


代码例如以下:

#include <cstdio>
#include <cstring>
char s[1100047];
int hash[247];
int main()
{
    __int64 n;
    while(~scanf("%I64d",&n) && n)
    {
        getchar();
        gets(s);
        __int64 len = strlen(s);
        memset(hash,0,sizeof(hash));
        __int64 i, star = 0, ans = 0, diff = 0;
        for(i = 0; i < len; i++)
        {
            ++hash[s[i]];//映射值+1
            if(hash[s[i]] == 1)//假设是第一次出现
            {
                diff++;//不同字母数+1
                if(diff > n)//假设超过n个不同字母
                {
                    while(--hash[s[star++]] > 0);//起点向右挪动 直到消去一个字母                  
					diff--;//不同字母数返回正常取值
                }
            }
            int LEN = i-star+1;//获得这个字符串长度
            ans = ans > LEN?ans:LEN;//保存最大
        }
        printf("%I64d
",ans);
    }
    return 0;
}



原文地址:https://www.cnblogs.com/mengfanrong/p/5018933.html