Topcoder SRM 627 div1 HappyLettersDiv1 : 字符串
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 |
|||||||||||||
|
|||||||||||||
Limits |
|||||||||||||
|
|||||||||||||
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) | |||||||||||||
|
|||||||||||||
| 1) | |||||||||||||
|
|||||||||||||
| 2) | |||||||||||||
|
|||||||||||||
| 3) | |||||||||||||
|
|||||||||||||
| 4) | |||||||||||||
|
|||||||||||||
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 : 字符串的更多相关文章
- Topcoder SRM 643 Div1 250<peter_pan>
Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*... ...
- Topcoder Srm 726 Div1 Hard
Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...
- 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)其 ...
- topcoder srm 630 div1 (2-SAT and SCC template)
problem1 link 首先计算任意两点的距离.然后枚举选出的集合中的两个点,判断其他点是否可以即可. problem2 link 假设字符串为$s$,长度为$n$.那么对于$SA$中的两个排名$ ...
- topcoder srm 545 div1
problem1 link 这个可以贪心地从前向后构造.假设当前已经的字符串为$S$,对于一个字符$c$来说,设将$c$加到$S$后得到的新串为$S^{'}$.那么如果$X+Y+Z \ge minIn ...
- topcoder srm 701 div1 -3
1.一堆石子有$n$个,Alice,Bob轮流拿,给定每个人每次可以拿的石子的数目的集合.谁先不能拿谁输.问谁能赢? 思路:对于先手来说,输赢的局面一定是从某个数字开始呈循环状态.所以找到这个循环开始 ...
- topcoder srm 714 div1
problem1 link 倒着想.每次添加一个右括号再添加一个左括号,直到还原.那么每次的右括号的选择范围为当前左括号后面的右括号减去后面已经使用的右括号. problem2 link 令$h(x) ...
- 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$. ...
- topcoder srm 706 div1
1.给定一个迷宫,点号表示不可行,井号表示可行.现在可以改变其中的一些井号的位置.问最少改变多少个井号可以使得从左上角到右下角存在路径. 思路:设高为$n$,宽为$m$,若井号的个数$S$小于$n+m ...
随机推荐
- 用StackExchange.Redis客户端连接阿里云Redis服务遇到的问题
阿里云推荐的Redis服务.NET客户端是ServiceStack.Redis,但ServiceStack.Redis不支持异步,不支持.NET Core,于是尝试使用StackExchange.Re ...
- 用JS识别各版本浏览器
自昨天发了各浏览器内核介绍的随笔,就闲不住了,想直接写个JS来识别用户所用浏览器版本. 写着写着却发现很多坑爹的地方,比如IE10-的版本是依循常规支持attachEvent,但到了IE11,却只支持 ...
- javascript 设计模式-----观察者模式
观察者模式在设计模式中被重点提到,因为它应用的场景非常多,而且在模块化设计当中扮演着非常重要的角色.MVC模式中最底层的就是观察者模式,当下流行的javascript框架backbone就是很好地运用 ...
- 初探ECMAScript6
基础变化 String类型新增了三个方法,不必使用indexOf来判断一个字符串是否在另一个字符串内 //String changes var a = "Hello world"; ...
- 正式工作的前奏——一个Java程序员的实习总结(1)
不知不觉,到深圳实习已经三个礼拜了.跟在暑假的三个半月实习不一样,这次收获更多,感受更好,算是摆脱了那次实习给我带来的阴影(这个会放到以后才说). 在知乎上,有这么一个问题,你现在最想跟刚工作时的你说 ...
- 实践基于Task的异步模式
Await 返回该系列目录<基于Task的异步模式--全面介绍> 在API级别,实现没有阻塞的等待的方法是提供callback(回调函数).对于Tasks来说,这是通过像ContinueW ...
- Oracle Redo 以及 Archived日志简述
Oracle通过Redo Archived实现数据的归档 什么是Redo日志 Redo日志记录了数据的变更,用于在数据库出现故障后,进行数据恢复. 功能主要由三个组件实现:Redo Log Buffe ...
- PHP 基础
var_dump(empty($a)); 判断变量是否为空 var_dump(isset($a)); 判断变量是否定义 $a=10;unset($a); 删除变量 var_d ...
- fir.im Weekly - 如何打造 Github 「爆款」开源项目
最近 Android 转用 Swift 的传闻甚嚣尘上,Swift 的 Github 主页上已经有了一次 merge>>「Port to Android」,让我们对 Swift 的想象又多 ...
- fir.im Weekly - 除了写代码,还需要了解什么
雾霾天,宜撸代码.吹牛,不宜出门约会(¬_¬)ノ 本期 fir.im Weekly 亦如往期,收集了优秀的 iOS/Android 开发资源,GitHub 源码.前端方面的热点分享.除了代码,也许你 ...