[Google Codejam] Round 1A 2016 - The Last Word
[Problem Description]
Problem
On the game show The Last Word, the host begins a round by showing the contestant a string S of uppercase English letters. The contestant has a whiteboard which is initially blank. The host will then present the contestant with the letters of S, one by one, in the order in which they appear in S. When the host presents the first letter, the contestant writes it on the whiteboard; this counts as the first word in the game (even though it is only one letter long). After that, each time the host presents a letter, the contestant must write it at the beginning or the end of the word on the whiteboard before the host moves on to the next letter (or to the end of the game, if there are no more letters).
For example, for S = CAB
, after writing the word C
on the whiteboard, the contestant could make one of the following four sets of choices:
- put the
A
beforeC
to formAC
, then put theB
beforeAC
to formBAC
- put the
A
beforeC
to formAC
, then put theB
afterAC
to formACB
- put the
A
afterC
to formCA
, then put theB
beforeCA
to formBCA
- put the
A
afterC
to formCA
, then put theB
afterCA
to formCAB
The word is called the last word when the contestant finishes writing all of the letters from S, under the given rules. The contestant wins the game if their last word is the last of an alphabetically sorted list of all of the possible last words that could have been produced. For the example above, the winning last word is CAB
(which happens to be the same as the original word). For a game with S = JAM
, the winning last word is MJA
.
You are the next contestant on this show, and the host has just showed you the string S. What's the winning last word that you should produce?
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each consists of one line with a string S.
Output
For each test case, output one line containing Case #x: y
, where x
is the test case number (starting from 1) and y
is the winning last word, as described in the statement.
Limits
1 ≤ T ≤ 100.
Small dataset
1 ≤ length of S ≤ 15.
Large dataset
1 ≤ length of S ≤ 1000.
Sample
Input |
Output |
7 |
Case #1: CAB |
今天是google的codejam contest的roundB,之前练习一下,就做了这道题,很简单,但是真正比赛的题目还是没有做出来。真正的算法大神是真的牛啊。
这道题就比较简单了。有点像二叉树嘛,由一个字符,每次插入到前面或后面就会生成两个字符串,由于最后需要字典序最大的,因此每次生成这两个字符串,我就保存最大的。
void LastWord(ifstream& fin) { int num;
fin >> num;
ofstream fout("output.txt");
for(int j=;j<num;j++){
string s1="";
string s2="";
string smax="";
string s;
fin >> s;
for (int i = ; i < s.length(); i++) {
s1 = smax + s[i];
s2 = s[i] + smax;
smax = s1 > s2 ? s1 : s2;
}
fout << "Case #" << j+ << ":" <<" "<<smax << endl;
}
return;
}
[Google Codejam] Round 1A 2016 - The Last Word的更多相关文章
- Google Codejam 2016 Round1A Problem C BFFs 简单图论
链接 Google Codejam 2016 Round1A Problem C BFFs 题意 n个小朋友要坐成一个圈.每个小朋友心中都有一个Best Friend Forever.要保证每个人的左 ...
- HYNB Round 8: 2016 ICPC Amritapuri Regionals
HYNB Round 8: 2016 ICPC Amritapuri Regionals A - Tim and BSTs 做法 经典的树 DP 问题. \(dp[u][i]\) 表示考虑以 u 为根 ...
- Google Code Jam Round 1A 2015 解题报告
题目链接:https://code.google.com/codejam/contest/4224486/ Problem A. Mushroom Monster 这题题意就是,有N个时间点,每个时间 ...
- Google Code Jam 2010 Round 1A Problem A. Rotate
https://code.google.com/codejam/contest/544101/dashboard#s=p0 Problem In the exciting game of Jo ...
- [Google Code Jam (Round 1A 2008) ] A. Minimum Scalar Product
Problem A. Minimum Scalar Product This contest is open for practice. You can try every problem as ...
- Google Code Jam Round 1A 2015 Problem B. Haircut 二分
Problem You are waiting in a long line to get a haircut at a trendy barber shop. The shop has B barb ...
- Google CodeJam 2016 round3-A.Teaching Assistant
题目描述: 原题是纯英文,大意是:你每天可以选择一门课去学习,选题和提交答案.题目为Coding或者Jamming.选的题目如果和老师选的一致,提交答案也匹配,最后可以得10分,若选题不一致只能得5分 ...
- 【二分答案】Google Code Jam Round 1A 2018
题意:有R个机器人,去买B件商品,有C个收银员,每个收银员有能处理的商品数量上限mi,处理单件商品所需的时间si,以及最后的装袋时间pi. 每个收银员最多只能对应一个机器人,每个机器人也最多只能对应一 ...
- 【贪心】Google Code Jam Round 1A 2018 Waffle Choppers
题意:给你一个矩阵,有些点是黑的,让你横切h刀,纵切v刀,问你是否能让切出的所有子矩阵的黑点数量相等. 设黑点总数为sum,sum必须能整除(h+1),进而sum/(h+1)必须能整除(v+1). 先 ...
随机推荐
- PageSlider中CSS3动画在除首屏之外先加载页面后执行动画的问题
PageSlider中CSS3动画在除首屏之外先加载页面后执行动画的问题,PageSlider中加入CSS3动画的话,默认只有首屏是从无到有执行动画,其他屏都是显示下页面再执行动画 这就造成其他屏的动 ...
- (cljs/run-at (JSVM. :all) "细说函数")
前言 作为一门函数式编程语言,深入了解函数的定义和使用自然是十分重要的事情,下面我们一起来学习吧! 3种基础定义方法 defn 定义语法 (defn name [params*] exprs*) 示 ...
- 初学c语言
虽然有一点点基础,但是还是从头学吧,这一周也就一些c语言的几个代码代表的意思和一个Hello world的程序. #include是头文件名,<>这是要返回的函数类型,然后是main主函数 ...
- js实现图片旋转、模板文件查看图片大图之记录篇[二]
一个小小的前端需求送给大家,使用js实现图片旋转,并且点击图片能够实现规定格式的大图. 主要使用的是jQuery的delegate()方法实现图片旋转,该方法主要的功能就是给某个组件绑定一个或一组事件 ...
- win7下elasticsearch5.0 安装head插件
项目开发用到了ES,5.X版本的,然而网上好多的安装资料都不能用,全是之前的老版本,今天弄了一上午终于完事了,总结一下安装的步骤. 1.安装NodeJs 去官网https://nodejs.org/e ...
- 4,JPA-Hibernate
一,什么是JPA JPA全称Java Persistence API.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. JPA(Java Pers ...
- nyoj_6:喷水装置(一)
要让总的使用到的装置数尽可能少,则可以贪心每次选取未使用的半径最大的装置 题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=6 #inclu ...
- Struts2简诉
Struts2框架是基于MVC模式的开源,MVC模式是一种开发方式,主要作用是对组件之间进行隔离,M代表业务逻辑层,V代表视图层,C代表控制层.有利于代码的后期维:Struts2框架的源码主要来于We ...
- ASP.NET MVC5 使用MiniProfiler 监控MVC性能
MiniProfiler ,一个简单而有效的迷你剖析器,可以有效的实时监控页面.通过直接引用.Ajax.Iframe形式访问的其它页面进行监控,监控内容包括数据库内容,并可以显示数据库访问的SQL. ...
- 【html】 a 标签
摘要 嗷呜,发现好多前端细节,基础不扎实啊,喵了个咪 target 属性 在制定框架中打开 <a href="a.html" target="view_frame& ...