Codeforces 137A-Postcards and photos(模拟+贪心)

本文介绍了一种物品整理策略,主人公需要将一系列明信片和照片依次放入柜子中,但每次只能拿取同类型物品且数量不超过五个。文章通过示例说明了如何采用贪心算法来最小化进入柜子的次数。
A. Postcards and photos
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarpus has postcards and photos hung in a row on the wall. He decided to put them away to the closet and hang on the wall a famous painter's picture. Polycarpus does it like that: he goes from the left to the right and removes the objects consecutively. As Polycarpus doesn't want any mix-ups to happen, he will not carry in his hands objects of two different types. In other words, Polycarpus can't carry both postcards and photos simultaneously. Sometimes he goes to the closet and puts the objects there, thus leaving his hands free. Polycarpus must put all the postcards and photos to the closet. He cannot skip objects. What minimum number of times he should visit the closet if he cannot carry more than 5 items?

Input

The only line of the input data contains a non-empty string consisting of letters "С" and "P" whose length does not exceed 100characters. If the i-th character in the string is the letter "С", that means that the i-th object (the numbering goes from the left to the right) on Polycarpus' wall is a postcard. And if the i-th character is the letter "P", than the i-th object on the wall is a photo.

Output

Print the only number — the minimum number of times Polycarpus has to visit the closet.

Sample test(s)
input
CPCPCPC
output
7
input
CCCCCCPPPPPP
output
4
input
CCCCCCPPCPPPPPPPPPP
output
6
input
CCCCCCCCCC
output
2
题意:有一堆明信片和照片,按顺序放在走廊里,C代表明信片,P代表照片,现在要把这堆东西拿到一个柜子里面去,规定每次只能拿一种物品且物品数目不能超过5,即每次只能拿C或S,而且最多拿5张,问最少需要拿多少次,按照题意模拟拿东西的过程即可,因为要求次数最少,所以只要每次拿尽可能多的物品数(贪心)即可。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <map>
using namespace std;
#define LL long long
int main()
{
	char s[110];
	while(gets(s))
	{
		int len=strlen(s),ans=0;
		for(int i=0;i<len;i++)
		{
			int hand=0;
			if(s[i]=='C')
			{
				for(int j=i+1;j<i+1+4&&j<len;j++)
				{
					if(s[j]=='P')
						break;
					hand++;
				}
			}
			else
			{
				for(int j=i+1;j<i+1+4&&j<len;j++)
				{
					if(s[j]=='C')
						break;
					hand++;
				}
			}
			i+=hand;
			ans++;
		}
		printf("%d\n",ans);
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值