【水题】TopCoder——SRM 556 ChocolateBar

这题的确是比较水,不过由于是第一次在topCoder上做题,要求提交的是一个类而不是完整的程序,所以多少有一点不适应。

不过这种形式我感觉还是挺好的,一定程度上提高了程序的可重用性。

没法在线评测也是一件很蛋疼的事……不过还好有标程和出题人的分析,这点比poj什么的强多了。

原题:

Problem Statement

 You just bought a very delicious chocolate bar from a local store. This chocolate bar consists of N squares, numbered 0 through N-1. All the squares are arranged in a single row. There is a letter carved on each square. You are given a String letters. The i-th character of letters denotes the letter carved on the i-th square (both indices are 0-based).

You want to share this delicious chocolate bar with your best friend. At first, you want to give him the whole bar, but then you remembered that your friend only likes a chocolate bar without repeated letters. Therefore, you want to remove zero or more squares from the beginning of the bar, and then zero or more squares from the end of the bar, in such way that the remaining bar will contain no repeated letters.

Return the maximum possible length of the remaining chocolate bar that contains no repeated letters.

Definition

 
Class:ChocolateBar
Method:maxLength
Parameters:String
Returns:int
Method signature:int maxLength(String letters)
(be sure your method is public)
 
 

Constraints

-letters will contain between 1 and 50 characters, inclusive.
-Each character of letters will be a lowercase letter ('a' - 'z').

Examples

0) 
 
"srm"
Returns: 3
You can give the whole chocolate bar as it contains no repeated letters.
1) 
 
"dengklek"
Returns: 6
Remove two squares from the end of the bar.
2) 
 
"haha"
Returns: 2
There are three possible ways:
  • Remove two squares from the beginning of the bar.
  • Remove two squares from the end of the bar.
  • Remove one square from the beginning of the bar and one square from the end of the bar.
3) 
 
"www"
Returns: 1
4) 
 
"thisisansrmbeforetopcoderopenfinals"
Returns: 9

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.     

我的程序:

#include<iostream>
#include<string.h>
#include<string>
using namespace std;

class ChocolateBar{
	
private:
	int len;
	int counter[30];
	bool flag[30];
	bool check;
public:
	int maxLength(string letters){
		len=letters.length();
		int maximum=0;
		for(int i=0;i<len;i++){
			for(int j=i;j<len;j++){
				memset(flag,false,sizeof(flag));
				check=true;
				for(int k=i;k<=j;k++){					
					if(flag[letters[k]-'a']==true){
						check=false;
						break;
					}
					else flag[letters[k]-'a']=true;
				}
				if(check==true&&(j-i+1)>maximum)
					maximum=j-i+1;
			}
		}
		return maximum;
	}//end method maxLength	
};

标准分析可以见 http://apps.topcoder.com/wiki/display/tc/SRM+556

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值