Topcoder SRM 687 (Div 2) 500.Quacking __ string matches

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

declare a string array;

 //"quack"

when first meet 'q',add to the string which index is supposed to be as small sa possible, unless add to a empty string from the array;

then meet 'u' we shall to find a string which is ended by q, 'a','c','k' are nearly the same as 'u';

else return -1; 


paste my code ☺


// 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!


                                                                                                                                                                                                                                                                 ------from ProLights

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值