[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 before C to form AC, then put the B before AC to form BAC
  • put the A before C to form AC, then put the B after AC to form ACB
  • put the A after C to form CA, then put the B before CA to form BCA
  • put the A after C to form CA, then put the B after CA to form CAB

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, TT 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
CAB
JAM
CODE
ABAAB
CABCBBABC
ABCABCABC
ZXCASDQWE
Case #1: CAB
Case #2: MJA
Case #3: OCDE
Case #4: BBAAA
Case #5: CCCABBBAB
Case #6: CCCBAABAB
Case #7: ZXCASDQWE

 

今天是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的更多相关文章

  1. Google Codejam 2016 Round1A Problem C BFFs 简单图论

    链接 Google Codejam 2016 Round1A Problem C BFFs 题意 n个小朋友要坐成一个圈.每个小朋友心中都有一个Best Friend Forever.要保证每个人的左 ...

  2. HYNB Round 8: 2016 ICPC Amritapuri Regionals

    HYNB Round 8: 2016 ICPC Amritapuri Regionals A - Tim and BSTs 做法 经典的树 DP 问题. \(dp[u][i]\) 表示考虑以 u 为根 ...

  3. Google Code Jam Round 1A 2015 解题报告

    题目链接:https://code.google.com/codejam/contest/4224486/ Problem A. Mushroom Monster 这题题意就是,有N个时间点,每个时间 ...

  4. 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 ...

  5. [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 ...

  6. 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 ...

  7. Google CodeJam 2016 round3-A.Teaching Assistant

    题目描述: 原题是纯英文,大意是:你每天可以选择一门课去学习,选题和提交答案.题目为Coding或者Jamming.选的题目如果和老师选的一致,提交答案也匹配,最后可以得10分,若选题不一致只能得5分 ...

  8. 【二分答案】Google Code Jam Round 1A 2018

    题意:有R个机器人,去买B件商品,有C个收银员,每个收银员有能处理的商品数量上限mi,处理单件商品所需的时间si,以及最后的装袋时间pi. 每个收银员最多只能对应一个机器人,每个机器人也最多只能对应一 ...

  9. 【贪心】Google Code Jam Round 1A 2018 Waffle Choppers

    题意:给你一个矩阵,有些点是黑的,让你横切h刀,纵切v刀,问你是否能让切出的所有子矩阵的黑点数量相等. 设黑点总数为sum,sum必须能整除(h+1),进而sum/(h+1)必须能整除(v+1). 先 ...

随机推荐

  1. Spring Security4实例(Java config 版) —— Remember-Me

    本文源码请看这里 相关文章: Spring Security4实例(Java config版)--ajax登录,自定义验证 Spring Security提供了两种remember-me的实现,一种是 ...

  2. requireJS 源码(三) data-main 的加载实现

    (一)入口 通过 data-main 去加载 JS 模块,是通过  req(cfg) 入口去进行处理的. 为了跟踪,你可以在此 加断点 进行调试跟踪. (二)  req({ })执行时,functio ...

  3. 阿里巴巴2018届应届生在线编程测验-研发工程师C/C++

    刚才去做了阿里巴巴的编程测验,好消息是抽到的题相对别的题简单一些,坏消息是编的太慢了,没有做完. 现在把题目和自己后来编出来的代码贴在这里,供大家参考. 题目: 1. 从命令行输入若干个正整数(大于等 ...

  4. laravel+vue组合的项目中引入ueditor(打包成组件形式)

    前言:最近写东西需要用到ueditor,并且需要是在vue组件中引入. (本博客默认你已经配置了laravel+vue的项目环境,如果还没有配置好的的小伙伴,可以看看我的另一篇博客,链接: http: ...

  5. c/c++ SQLite3的常用使用方法;

    下面测试用的sqlite例子;大家可以参考使用; #include "CppSQLite3.h" Class TestSqlite{ //定义db指针 private: CppSQ ...

  6. 【转载】 ISO14229系列之一:简介

    转载链接:http://www.cnblogs.com/autogeek/p/4458591.html 前言 由于工作中经常用到ISO-14229,因此决定对该协议做个总体介绍和总结,既是对自己学习的 ...

  7. 个人作业2--英语学习APP案例分析

    1.下载APP并使用,上手体验 个人很喜欢这种风格,画面简洁,排版精细,尤其是联想词的界面,很惊喜.但是很多链接比如精选文章点进去之后的UI设计并不理想,感觉只是一个网页而已.并且我不能够保存或者收藏 ...

  8. MD5加密Demo

    package com.util; import java.security.MessageDigest; public class MD5 { public final static String ...

  9. (转)Linux文件系统的实现

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Linux文件管理从用户的层面介绍了Linux管理文件的方式.Linux有一个树状 ...

  10. ubuntu下处理mysql无法启动故障一例

    故障现象: mysql无法启动 1: dmesg |grep mysql [101353.820000] init: mysql post-start process (9077) terminate ...