这题的确是比较水,不过由于是第一次在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 | |||||||||||||
| |||||||||||||
Constraints | |||||||||||||
- | letters will contain between 1 and 50 characters, inclusive. | ||||||||||||
- | Each character of letters will be a lowercase letter ('a' - 'z'). | ||||||||||||
Examples | |||||||||||||
0) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
| |||||||||||||
3) | |||||||||||||
| |||||||||||||
4) | |||||||||||||
|
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