[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. java equals和==区别及string类的说明

    一.equals和==的区别 1.1.equals之string字符串的比较 1.1.1.源码如下图 if (this == anObject) {            return true;  ...

  2. poj 1948 Triangular Pastures 小结

    Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The o ...

  3. 第2篇:用as3.0制作一个滚动条组件

    本实例演示了实现一个滚动条基本功能的制作方法,没有添加改变皮肤,修改滚动条视框大小等功能,有兴趣的朋友可根据自己要求自行添加.使用时只需要通过以下一行代码创建滚动条组件: var myScrollba ...

  4. 简单的线性回归问题-TensorFlow+MATLAB·

    首先我们要试验的是 人体脂肪fat和年龄age以及体重weight之间的关系,我们的目标就是得到一个最优化的平面来表示三者之间的关系: TensorFlow的程序如下: import tensorfl ...

  5. 关于sessionStorage的移动端兼容问题

    最近在开发移动端项目时,需要用到的本地存储的地方不少.都是一些只要记住当前打开窗口的用户数据就行,所以我选择用的sessionStorage.使用场景如下: A.html页面需要记录一条数据{a:1, ...

  6. DOM事件代码小结

    以下代码出自<DOM Enlightenment>一书1.三种事件形式 <body onclick="alert('触发内联属性事件')"> <div ...

  7. Android - 读取XML文件中的数据

    读取XML中存储的数据.将xmlfile.xml存放在assets文件夹中.在activity_main.xml中创建一个textview,用来显示读取到的数据. XML文件内容如下:xmlfile. ...

  8. 51nod_1605:棋盘问题

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1605 题目中最有用的点其实还是x必为奇数 #include& ...

  9. 替换Spring Boot 的EnableCaching注解

    SpringBoot 中可使用@Cacheable注解来更方便的使用redis,这个注解是通过拦截器工作的,使用了@Cacheable的方法执行时,执行到CglibAopProxy.java中的 Dy ...

  10. .babelrc 文件

    文件干啥用的 babel是降es6转义成浏览器能理解的es5语法. 如果项目中用了babel 转移,需要定义babel需要的插件和预设转码. babel 一般可以配合 webpack . browse ...