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!

时间: 2024-08-07 17:52:27

Topcoder SRM 687 (Div 2) 500.Quacking的相关文章

Topcoder SRM 687 (Div 2) 250.Quorum

Problem Statement   In one organization they have n different committees. The organization has a very large number of employees. Each employee is a member of each committee. Each committee has a quorum: the smallest number of members that have to be

TopCoder SRM 639 Div.2 500 AliceGameEasy

题意: 一个游戏有n轮,有A和B比赛,谁在第 i 轮得胜,就获得 i 分,给出x,y,问A得x分,B得y分有没有可能,如果有,输出A最少赢的盘数 解题思路: 首先判断n(n+1)/2 = (x+y)是否有解,即是否存在n为整数,使得所有分数的和加起来为x+y,即判断n2+n-2(x+y)=0,存在整数解, 根据二次方程的根为(-1±Δ)/2 为整数,其中Δ=√(1+8(x+y)) , 即判断1+8(x+y)是否能开方以及Δ是否为奇数(如果Δ为偶数,则根不是整数) 如果前面条件满足,在通过贪心,从

TopCoder SRM 639 Div.2 500 AliceGameEasy --乱搞

题意: 一个游戏有n轮,有A和B比赛,谁在第 i 轮得胜,就获得 i 分,给出x,y,问A得x分,B得y分有没有可能,如果有,输出A最少赢的盘数. 解法: 这题是我傻逼了,处理上各种不优越,要使n*(n+1)/2 >= 10^12, n为10^6是不够的,要开大一点,总是细节地方不注意. 做法很简单,先用map或者其他什么东西判断x+y是否为某个n*(n+1)/2, 如果不是,那肯定为-1,再就是x=0有可能要单独考虑,然后就是选一些数凑成x,由于要最少,那么从大的开始凑起,可以暴力地凑,也可以

TopCoder SRM 633 Div.2 500 Jumping

题意:给一个点(x,y),给一些步长delta1,delta2...deltaN,问从(0,0)严格按照步长走完N步后能否正好到达(x,y)点. 解法:其实就是判断这些线段和(0,0)-(x,y)这条线段能否构成一个多边(角?)形的问题,只需判断最长的边是否不大于于所有边长和的一半即可. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #inclu

TopCoder SRM 634 Div.2[ABC]

TopCoder SRM 634 Div.2[ABC] ACM 题目地址: TopCoder SRM 634 赛后做的,感觉现场肯定做不出来Orz,简直不能多说. Level One-MountainRanges[水题] 题意: 问序列中有几个完全大于旁边的峰. 分析: 傻逼题,不多说. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * File: one.cpp * Create Date: 2014-09-26 21:01:23 * Desc

TopCoder SRM 628 DIV 2

250-point problem Problem Statement    Janusz is learning how to play chess. He is using the standard chessboard with 8 rows and 8 columns. Both the rows and the columns are numbered 0 through 7. Thus, we can describe each cell using its two coordina

topcoder srm 628 div2 250 500

做了一道题,对了,但是还是掉分了. 第二道题也做了,但是没有交上,不知道对错. 后来交上以后发现少判断了一个条件,改过之后就对了. 第一道题爆搜的,有点麻烦了,其实几行代码就行. 250贴代码: 1 #include <iostream> 2 #include <cstring> 3 #include <queue> 4 #include <cmath> 5 #include <cstdio> 6 #include <algorithm&g

TopCoder SRM 560 Div 1 - Problem 1000 BoundedOptimization &amp; Codeforces 839 E

传送门:https://284914869.github.io/AEoj/560.html 题目简述: 定义"项"为两个不同变量相乘. 求一个由多个不同"项"相加,含有n个不同变量的式子的最大值. 另外限制了每一个变量的最大最小值R[i]和L[i]和所有变量之和的最大值Max. n<=13 题外话: 刚开始做这道题的时候,感觉意外眼熟? codeforces 839 E(此题的退化版):http://codeforces.com/contest/839/pro

[topcoder]SRM 633 DIV 2

第一题,http://community.topcoder.com/stat?c=problem_statement&pm=13462&rd=16076 模拟就可以了. #include <vector> #include <algorithm> using namespace std; class Target { public: vector <string> draw(int n) { vector<string> result(n,