Broken Keyboard 2010.3.6

Broken Keyboard 2010.3.6

BrokenKeyboard

【关键字】

模拟+队列+游标+哈希

【摘要】

给定一个很长很长的序列,求仅存在m个字符的最长子序列。

【正文】

1、题目描述

Broken Keyboard

Time Limit:2000MS Memory Limit:65536K

Total Submit:29 Accepted:9

Description

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

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

Input

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

The last test case is followed by a linecontaining one zero.

Output

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

SampleInput

5

This can't be solved by brute force.

1

Mississippi

0

SampleOutput

7

2

Hint

The largest substring for the first testcase is "_by_bru", where _ stands for a space character.

Source

ULM 2008

2、算法分析

按照顺序一个一个字符的读入,根据情况进行队列的进出。

设置两个游标,head,tail;used存当前已经出现的字符的种类;设置哈希表hash[]存字符出现的次数。没读取一个字符,如果原来没有出现过,而且used<goal,则,used++,并将此字符入队;如果used=goal,则head后移,并将移除的字符的hash相应减一,直至见到某个字符的hash==0时,说明used==goal-1,此时,新出现的字符就可以入列了。如果新读入的字符时原来出现过了,那么,就可以直接放入队列,并且更新hash。

当used==goal,且读入没出现的字符的时候,就看tail-head是不是>ans,是的话,就更新。

这道题,用到了游标动态处理,还有hash来存出现的次数和是否出现,很神奇~~~比赛时没有想到,虽然只是简单的模拟之类的,但是,想不到的话,真的不知道要用游标~~~强大的游标~~~

3、源码

#include <stdio.h>
#include <string.h>

#define MAXN 1000000+10
#define HASHN 300

char q[MAXN],ch;
int hash[HASHN],head,goal,tail,ans,now,used;

void putq()
{
	tail++;
	q[tail]=ch;
	hash[(int)ch]++;
}

void main()
{
	while (scanf("%d",&goal),goal)
	{
		getchar();
		head=0;
		tail=0;
		used=0;
		ans=0;
		memset(hash,0,sizeof(hash));
		while (scanf("%c",&ch),ch!='\n')
		{
			if (hash[(int)ch]!=0)
			{
				putq();
			}
			else
			{
				if (used<goal)
				{
					putq();
					used++;
				}
				else
				{
					if (tail-head>ans) ans=tail-head;
					while (1)
					{
						head++;
						hash[(int)q[head]]--;
						if (hash[(int)q[head]]==0)
						{
							break;
						}
					}
					putq();
				}
			}
		}
		if (tail-head>ans) ans=tail-head;
		printf("%d\n",ans);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值