标签:
Problem Statement |
|||||||||||||
Ducks have started mysteriously appearing in your room. All ducks make the same sound: "quack". Each duck makes the sound one or more times, one after another. For example, valid sounds for a single duck are "quack", "quackquackquackquack", "quackquack", etc. You have lost count of how many ducks are in your room. The ducks are quacking, and the sounds of their quacks are getting mixed up. You have recorded the sounds, obtaining a string of characters. When you later listened to the recording, you realized that the quacking of each individual duck forms a (not necessarily contiguous) subsequence of the recording. You also noticed that each letter in the recording belongs to exactly one duck. For example, if there were two ducks, you may have recorded "quqacukqauackck". You are given a string s that contains an arbitrary recording. Find and return the smallest number of ducks that could have produced the recording. Note that it is possible that the given recording is not a valid recording of quacking ducks. In such case, return -1. |
|||||||||||||
Definition |
|||||||||||||
|
|||||||||||||
Limits |
|||||||||||||
|
|||||||||||||
Constraints |
|||||||||||||
- | s will have between 5 and 2,500 characters, inclusive. | ||||||||||||
- | Each character of s will be ‘q‘, ‘u‘, ‘a‘, ‘c‘, or ‘k‘. | ||||||||||||
Examples |
|||||||||||||
0) | |||||||||||||
|
|||||||||||||
1) | |||||||||||||
|
|||||||||||||
2) | |||||||||||||
|
|||||||||||||
3) | |||||||||||||
|
|||||||||||||
4) | |||||||||||||
|
|||||||||||||
5) | |||||||||||||
|
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.
My Solution
I will add the explanation tomorrow
paste my code first ?
// BEGIN CUT HERE // END CUT HERE #line 5 "Quacking.cpp" #include <string> #include <vector> using namespace std; class Quacking { public: string str[501]; // 2500/5 => 500 int quack(string s) { //"quack" int sz = s.size(), smallsz, crr = -1; for(int i = 0; i < sz; i++){ crr = -1; if(s[i] == 'q'){ for(int j = 0; j < 500; j++){ smallsz = str[j].size(); if(smallsz == 0) {str[j] = 'q'; crr = 1; break;} else if(str[j][smallsz-1] == 'k') {str[j] += 'q'; crr = 1; break;} } if(crr == -1) return -1; } else if(s[i] == 'u'){ for(int j = 0; j < 500; j++){ smallsz = str[j].size(); if(smallsz == 0) {return -1;} else if(str[j][smallsz-1] == 'q') {str[j] += 'u'; crr = 1; break;} } if(crr == -1) return -1; } else if(s[i] == 'a'){ for(int j = 0; j < 500; j++){ smallsz = str[j].size(); if(smallsz == 0) {return -1;} else if(str[j][smallsz-1] == 'u') {str[j] += 'a'; crr = 1; break;} } if(crr == -1) return -1; } else if(s[i] == 'c'){ for(int j = 0; j < 500; j++){ smallsz = str[j].size(); if(smallsz == 0) {return -1;} else if(str[j][smallsz-1] == 'a') {str[j] += 'c'; crr = 1; break;} } if(crr == -1) return -1; } else if(s[i] == 'k'){ for(int j = 0; j < 500; j++){ smallsz = str[j].size(); if(smallsz == 0) {return -1;} else if(str[j][smallsz-1] == 'c') {str[j] += 'k'; crr = 1; break;} } if(crr == -1) return -1; } } int ans = 0; for(int i = 0; i < 500; i++){ if(str[i].size()) ans++; } return ans; } };
Topcoder SRM 687 (Div 2) 500.Quacking
标签:
原文地址:http://blog.csdn.net/prolightsfxjh/article/details/51158414