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. 可在广域网部署运行的QQ高仿版 -- GG叽叽V2.4,增加远程协助、桌面共享功能(源码)

    QQ的远程协助.或者说桌面共享是一个非常实用的功能,所以,2.4版本的GG复制了它,而且,GG增强了桌面共享的功能,它可以允许指定要共享桌面的区域,这样,对方就只能看到指定区域的桌面,这对节省流量会非 ...

  2. Windows Phone App的dump 文件分析

    前言 我们在发布了自己的App以后,Windows Phone的Error Report机制会帮助我们收集程序的崩溃信息并发送到微软的服务器上,这可以辅助开发者提高App的稳定性. 那么如何利用这些d ...

  3. dijit样式定制(二)dijit.form.Select与dijit.form.NumberSpinner

    dijit.form.Select: Select的样式位于Claro/form/Select.less中,Select主要通过table来布局,下图可以看到Select的布局结构 介绍几个主要的cl ...

  4. tsd-提升IDE对JavaScript智能感知的能力

    在编写前端JavaScript代码时,最痛苦的莫过于代码的智能感知(Intelli Sense). 追其根源,是因为JavaScript是一门弱类型的动态语言.对于弱类型的动态语言来说,智能感知就是I ...

  5. Nim教程【七】

    这是国内第一个关于Nim的系列教程 先说废话 很开心,在今天凌晨快一点多的时候拿到了 nim-lang.com:nim-lang.cn:nim-lang.net 这三个域名,到不是为了投资,准备用ni ...

  6. java提高篇(二五)-----HashTable

          在java中与有两个类都提供了一个多种用途的hashTable机制,他们都可以将可以key和value结合起来构成键值对通过put(key,value)方法保存起来,然后通过get(key ...

  7. linux奇技淫巧

    用着用着就发现,linux的每个命令都是那么的深奥而富有技巧,实用而淫荡..真可谓奇技淫巧.... 初学的真不易掌握... http://www.cnblogs.com/include/archive ...

  8. 腾讯Ubuntu云虚拟主机设置ftp服务器

    刚申请了免费的腾讯云主机, 发现还要想办法自己的服务器代码传到云主机上 在网上搜了很多方法介绍, 照着设置完后都无法正常连接 最后半夜尿醒来睡不着找到一篇站内文章, 提到必须注释掉一行代码 这个是其他 ...

  9. Atitit 马尔可夫过程(Markov process) hmm隐马尔科夫。 马尔可夫链,的原理attilax总结

    Atitit 马尔可夫过程(Markov process) hmm隐马尔科夫. 马尔可夫链,的原理attilax总结 1. 马尔可夫过程1 1.1. 马尔科夫的应用 生成一篇"看起来像文章的 ...

  10. 阿里云配置mysql navcat远程连接

    默认是不能用客户端远程连接的,阿里云提供的help.docx里面做了设置说明,mysql密码默认存放在/alidata/account.log 首先登录: mysql -u root -h local ...