Topcoder SRM 687 (Div 2) 500.Quacking

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

 
Class: Quacking
Method: quack
Parameters: string
Returns: int
Method signature: int quack(string s)
(be sure your method is public)

Limits

 
Time limit (s): 2.000
Memory limit (MB): 256
Stack limit (MB): 256

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)  
 
"quqacukqauackck"
Returns: 2
This is the same as the one from the problem statement.
1)  
 
"kcauq"
Returns: -1
A backward duck is not a real duck.
2)  
 
"quackquackquackquackquackquackquackquackquackquack"
Returns: 1
A single duck can make arbitrarily many quack sounds.
3)  
 
"qqqqqqqqqquuuuuuuuuuaaaaaaaaaacccccccccckkkkkkkkkk"
Returns: 10
4)  
 
"quqaquuacakcqckkuaquckqauckack"
Returns: 3
This is one possible solution with 3 ducks.
Mixed: quqaquuacakcqckkuaquckqauckack
Duck1: ____q_u__a___ck_______________
Duck2: __q__u_ac_k_q___ua__ckq_u__ack
Duck3: qu_a_______c___k__qu___a_ck___
Here, we can verify that each letter comes from exactly one duck.
5)  
 
"quackqauckquack"
Returns: -1
Note that the second quack is misspelled.

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;
	}
};

Thank you!


你可能感兴趣的:(String,ACM,topcoder,SRM,687)