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. Aoite 系列(01) - 比 Dapper 更好用的 ORM

    Aoite 是一个适于任何 .Net Framework 4.0+ 项目的快速开发整体解决方案.Aoite.Data 适用于市面上大多数的数据库提供程序,通过统一封装,可以在日常开发中简单便捷的操作数 ...

  2. 新版markdown功能发布!支持github flavored markdown!

    让大家久等了!新版markdown功能一直拖到今天才发布,很是愧疚...但不管怎么样,总算发布了! 今年1月份发布第一版markdown功能之后,很多园友反馈说做得很烂,我们综合大家的反馈之后发现不仅 ...

  3. [ACM_几何] Fishnet

      http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28417#problem/C 本题大意:有一个1X1的矩形,每边按照从小到大的顺序给n ...

  4. 翻译-Salt与Ansible全方位比较

    原文链接:http://jensrantil.github.io/salt-vs-ansible.html 作者: Jens Rantil 之前某些时候我需要评估配置管理系统.结合从他人得到的意见,我 ...

  5. 【重要更新】Senparc.Weixin SDK v4.3.3升级说明

    为了更好地适应微信越来越快的API更新速度和越来越多的API数量,本次Senparc.Weixin.dll v4.3.3对一些通用功能进行了深度的重构. 本次更新同时影响以下所有Senparc.Wei ...

  6. Git学习笔记(7)——多人协作

    本文主要记录了,多人协作时,产生冲突时的解决情况. 多人环境创建 首先我们需要模拟一个多人环境.前面的Git的学习都是在Ubuntu上面,现在我们也搭建一个win环境吧.安装win环境下的Git,很简 ...

  7. VS2013 好用的插件

    切换到vs2013上有些时间了,以下是我个人认为比较好的插件. Resharper 神器中的神器,提升编码效率的第一神器,附带提高编码能力:除去臃肿的体积,堪称完美: Productivity Pow ...

  8. Redis教程(十四):内存优化介绍

    转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/142.html 一.特殊编码: 自从Redis 2.2之后,很多数据类型都 ...

  9. JS动态设置css的几种方式

    1. 直接设置style的属性  某些情况用这个设置 !important值无效 如果属性有'-'号,就写成驼峰的形式(如textAlign)  如果想保留 - 号,就中括号的形式  element. ...

  10. lua的私有性(privacy)

    很多人认为私有性是面向对象语言的应有的一部分.每个对象的状态应该是这个对象自己的事情.在一些面向对象的语言中,比如C++和Java你可以控制对象成员变量或者成员方法是否私有.其他一些语言比如Small ...