Topcoder SRM 627 div1 HappyLettersDiv1: string

Source: Internet
Author: User

Problem Statement
    

The Happy letter game is played as Follows:at the beginning, several play ERs enter the field. Each player has a lowercase 中文版 in their back. The game is played in turns. In each turn, you select the players with different letters, and both selected players the field. The game ends once it is impossible to take another turn.

If There is some players left in the field at the end of the game, and they must all has the same letter. That's called the winning letter. If There is no players left in the field at the end of the game, there is no winning letter.

You are given a string  letters . The characters in  Letters  are The characters carried by the players at the beginning of the GAM E. Return a string with all possible winning letters. The letters in the returned string must is sorted in increasing order.

Definition
Class: HappyLetterDiv1
Method: Gethappyletters
Parameters: String
Returns: String
Method Signature: String Gethappyletters (String letters)
(Be sure your method was public)
Limits
Time limit (s): 2.000
Memory Limit (MB): 256
Notes
- If there ' s no happy letter, return the empty string.
Constraints
- Letters'll contain between 1 and elements.
- Each element of letters would be a lowercase 中文版 (' A '-' Z ').
Examples
0)
 "AABBACCCC" 
 Returns: "abc" 
each of the three letters can be the-winning letter. H Ere is one possibility what ' a ' can be the The winning Letter:let's number the players 0 through 8 in the order in which they appear in the input. We can then play the game as follows:
  • Send away players 1 (' a ') and 8 (' C ').
  • Send away Players 2 (' B ') and 6 (' C ').
  • Send away Players 7 (' C ') and 0 (' a ').
  • Send away Players 5 (' C ') and 3 (' B ').
  • the only player to be player 4 (' a '), hence ' a ' is the winning letter.
1)
"AAAAAAACCDD"
Returns: "A"
Only the letter ' a ' can win.
2)
"Ddabccadb"
Returns: "ABCD"
3)
"AAABBB"
Returns: ""
No letter can win.
4)
"Rdokcogscosn"
Returns: "Cos"

Mean:

Give you a string of only lowercase letters, each round you can choose two different characters to delete, if there is the last remaining characters, then this character is winning letter, now you want to return a string that may be winning letter characters, and sorted in ascending order.

Analyse:

We first count the number of occurrences of each letter, and then the 26 lowercase letters to judge, if not to include itself, the maximum number of that character is greater than the number of characters remaining, it is impossible to meet the requirements of the topic (different characters match), otherwise meet the requirements of the topic, add the ANS string can be.

Time Complexity: O (N)

Source Code:

BEGIN cut here//END cut here#line 5 "HappyLetterDiv1.cpp"//memory time//K ms#include<algorithm> #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <vector># include<queue> #include <stack> #include <iomanip> #include <string> #include <climits># include<cmath> #define MAX 1100#define LL long longusing namespace Std;class HappyLetterDiv1 {public:string g        Ethappyletters (String letters) {string ans;        Ans.clear ();        int len=letters.size ();        int cnt[30]={0};        for (int i=0;i<len;i++) cnt[letters[i]-' a ']++; for (int i=0;i<26;i++) {if (Cnt[i]) {if (cnt[i]==1&&! (                len&1)) continue;                int maxx=-1;                for (int j=0;j<26;j++) if (j!=i) Maxx=max (Maxx,cnt[j]);         if (Maxx<=len-1-maxx)           ans+= ' a ' +i;    }} return ans; }};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.