Problem Statement

    

The Happy Letter game is played as follows: At the beginning, several players enter the field. Each player has a lowercase English letter on their back. The game is played in turns. In each turn, you select two players with different letters, and both selected players leave the field. The game ends once it is impossible to take another turn.

If there are some players left in the field at the end of the game, they must all have the same letter. That letter is called the winning letter. If there are 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 game. Return a string with all possible winning letters. The letters in the returned string must be sorted in increasing order.

Definition

    
Class: HappyLetterDiv1
Method: getHappyLetters
Parameters: string
Returns: string
Method signature: string getHappyLetters(string letters)
(be sure your method is public)

Limits

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

Notes

- If there's no happy letter, return the empty string.

Constraints

- letters will contain between 1 and 50 elements.
- Each element of letters will be a lowercase English letter ('a'-'z').

Examples

0)  
    
"aabbacccc"
Returns: "abc"
Each of the three letters can be the winning letter. Here is one possibility how 'a' can be 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 left is player 4 ('a'), hence 'a' is the winning letter.
1)  
    
"aaaaaaaccdd"
Returns: "a"
Only letter 'a' can win.
2)  
    
"ddabccadb"
Returns: "abcd"
 
3)  
    
"aaabbb"
Returns: ""
No letter can win.
4)  
    
"rdokcogscosn"
Returns: "cos"
 

Mean:

给你一个只有小写字母组成的字符串,每一轮你可以选择两个不相同的字符删去,如果最后还有剩下的字符,那么这个字符就是winning letter,现在要你返回可能是winning letter的字符组成的字符串,并按照升序排序。

analyse:

我们首先将每个字母出现的次数统计一遍,然后就是对26个小写字母进行判断,如果不包括本身,最多数量的那个字符的数量大于剩余字符的数量,说明不可能满足题目的要求(不同的字符相互匹配),否则就符合题目要求,加入ans字符串即可。

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 long
using namespace std; class HappyLetterDiv1 {
public:
string getHappyLetters(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;
}
};

  

Topcoder SRM 627 div1 HappyLettersDiv1 : 字符串的更多相关文章

  1. Topcoder SRM 643 Div1 250<peter_pan>

    Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*... ...

  2. Topcoder Srm 726 Div1 Hard

    Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...

  3. topcoder srm 575 div1

    problem1 link 如果$k$是先手必胜那么$f(k)=1$否则$f(k)=0$ 通过对前面小的数字的计算可以发现:(1)$f(2k+1)=0$,(2)$f(2^{2k+1})=0$,(3)其 ...

  4. topcoder srm 630 div1 (2-SAT and SCC template)

    problem1 link 首先计算任意两点的距离.然后枚举选出的集合中的两个点,判断其他点是否可以即可. problem2 link 假设字符串为$s$,长度为$n$.那么对于$SA$中的两个排名$ ...

  5. topcoder srm 545 div1

    problem1 link 这个可以贪心地从前向后构造.假设当前已经的字符串为$S$,对于一个字符$c$来说,设将$c$加到$S$后得到的新串为$S^{'}$.那么如果$X+Y+Z \ge minIn ...

  6. topcoder srm 701 div1 -3

    1.一堆石子有$n$个,Alice,Bob轮流拿,给定每个人每次可以拿的石子的数目的集合.谁先不能拿谁输.问谁能赢? 思路:对于先手来说,输赢的局面一定是从某个数字开始呈循环状态.所以找到这个循环开始 ...

  7. topcoder srm 714 div1

    problem1 link 倒着想.每次添加一个右括号再添加一个左括号,直到还原.那么每次的右括号的选择范围为当前左括号后面的右括号减去后面已经使用的右括号. problem2 link 令$h(x) ...

  8. topcoder srm 702 div1 -3

    1.给定一个$n*m$的矩阵,里面的数字是1到$n*m$的一个排列.一个$n*m$矩阵$A$对应一个$n*m$ 的01字符串,字符串的位置$i*m+j$为1当且仅当$A_{i,j}=i*m+j+1$. ...

  9. topcoder srm 706 div1

    1.给定一个迷宫,点号表示不可行,井号表示可行.现在可以改变其中的一些井号的位置.问最少改变多少个井号可以使得从左上角到右下角存在路径. 思路:设高为$n$,宽为$m$,若井号的个数$S$小于$n+m ...

随机推荐

  1. node(md5)

    md5是一种信息-摘要算法,即针对一段明文给出一个hash值,在密码学中最经典的用法是验证数据的完整性,因为一旦原始数据发生改变那么生成的摘要也必将不同. 网络中md5可以用于用户密码的加密,即在数据 ...

  2. js跨浏览器的事件处理函数

    /* 跨浏览器的事件处理函数 */ var EventUtil = { addHandler : function(element,type,handler){ if(element.addEvent ...

  3. 赴美工作常识(Part 6 - 绿卡排队)

    上一篇<赴美工作常识(Part 5 - 绿卡优先级)>解释完排队的优先级是怎么确定的,以及 PERM 和 I–140 表的意义,接下来就要解释一下队具体是怎么排的以及排到之后的 I–485 ...

  4. 翻译-使用Spring WebService生成SOAP Web Service

    原文链接:http://spring.io/guides/gs/producing-web-service/ 生成SOAP web service 该指南将带领你使用Spring创建一个基于SOAP的 ...

  5. navigationController pop的几种方法

    一,popViewControllerAnimated [self.navigationController popViewControllerAnimated:YES]; 二,popToRootVi ...

  6. CSS水平垂直居中的几种方法2

    直接进入主题! 一.脱离文档流元素的居中 方法一:margin:auto法 CSS代码: div{ width: 400px; height: 400px; position: relative; b ...

  7. 第九回 Microsoft.Practices.Unity.Interception实现基于数据集的缓存(针对六,七,八讲的具体概念和配置的解说)

    返回目录 概念 Microsoft.Practices.Unity.Interception是一个拦截器,它隶属于Microsoft.Practices.Unity组成之中,主要完成AOP的功能,而实 ...

  8. Beats数据采集---Packetbeat\Filebeat\Topbeat\WinlogBeat使用指南

    Beats是elastic公司的一款轻量级数据采集产品,它包含了几个子产品: packetbeat(用于监控网络流量). filebeat(用于监听日志数据,可以替代logstash-input-fi ...

  9. 数据库MySQL开篇

    作为一名前端,后天方面的知识虽说不一定要精通后台但是对后台方面的知识有一定了解是非常必要的. 今天先说一些概念方面的,因本人才疏学浅,对于数据库这方面的自学相当于摸着石头过河,有什么写的不对的地方,希 ...

  10. 理解 Lua 的那些坑爹特性

    按:最近看到了依云的文章,一方面,为Lua被人误解而感到十分难过,另一方面,也为我的好友, 依云没有能够体会到Lua的绝妙和优雅之处而感到很遗憾,因此我写了这篇文章,逐条款地说明了 依云理解中出现的一 ...