(http://leetcode.com/2011/01/studious-student-problem-analysis.html)

You've been given a list of words to study and memorize. Being a diligent student of language and the arts, you've decided to not study them at all and instead make up pointless games based on them. One game you've come up with is to see how you can concatenate the words to generate the lexicographically lowest possible string.

Input:

As input for playing this game you will receive a text file containing an integer N, the number of word sets you need to play your game against. This will be followed by N word sets, each starting with an integer M, the number of words in the set, followed by M words. All tokens in the input will be separated by some whitespace and, aside from N and M, will consist entirely of lowercase letters.

Output:

Your submission should contain the lexicographically shortest strings for each corresponding word set, one per line and in order.

Constraints:

1 <= N <= 100

1 <= M <= 9

1 <= all word lengths <= 10

--

It's not right to sort and concatenate each individual word together to form the lexicographically smallest string. For example, inputs are "zza zz".

If no word appears to be a prefix of any other words, then the simple sort + concatenate must yield the smallest dictionary order string.

We re-define the order relation of two words, s1 and s2, as:

s1 is less than s2 iff
s1 + s2 < s2 + s1

Code:

bool compareSort(const string& s1, const string& s2)
{
return s1 + s2 < s2 + s1;
} int main()
{
string words[];
int N, M; cin >> N;
for (int i = ; i < N; i++)
{
cin >> M; for (int j = ; j < M; i++)
cin >> words[j]; sort(words, words+M, compareSort); for (int j = ; j < M; j++)
cout << words[j];
cout << endl;
}
}

Studious Student Problem Analysis的更多相关文章

  1. Educational Codeforces Round 34 A. Hungry Student Problem【枚举】

    A. Hungry Student Problem time limit per test 1 second memory limit per test 256 megabytes input sta ...

  2. 903A. Hungry Student Problem#饥饿的学生(暴力&双层枚举)

    题目出处:http://codeforces.com/problemset/problem/903/A 题目大意就是:一个数能否用正整数个另外两个数合成 #include<iostream> ...

  3. A Complete Tutorial on Tree Based Modeling from Scratch (in R & Python)

    A Complete Tutorial on Tree Based Modeling from Scratch (in R & Python) MACHINE LEARNING PYTHON  ...

  4. [转]NLP Tasks

    Natural Language Processing Tasks and Selected References I've been working on several natural langu ...

  5. cvpr2015papers

    @http://www-cs-faculty.stanford.edu/people/karpathy/cvpr2015papers/ CVPR 2015 papers (in nicer forma ...

  6. Atitit 编程语言编程方法的进化演进 sp  COP ,AOP ,SOP

    Atitit 编程语言编程方法的进化演进 sp  COP ,AOP ,SOP 1.1.  Sp  oop>>COP ,AOP ,SOP1 1.2. Sp  oop 结构化方法SP(Stru ...

  7. leetcode 111 minimum depth of binary tree

    problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...

  8. 发布一个免费开源软件-- PAD流程图绘制软件PADFlowChart

    软件的可执行文件下载:PADFlowChart-exe.zip MD5校验码:91FCA9FEC9665FD09BEB3DA94ADC1CE6 SHA1校验码:ECD742AA3092A085AB07 ...

  9. 北大poj-1081

    You Who? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 801   Accepted: 273 Descriptio ...

随机推荐

  1. Windows Azure 网站:应用程序字符串和连接字符串的工作原理

    编辑人员注释:本文章由 Windows Azure 网站团队的首席项目经理 Stefan Schackow 撰写. Windows Azure 网站上有一个方便的功能,即开发人员可将 Azure 中的 ...

  2. 深入理解java虚拟机---读后笔记(垃圾回收)

    运行时数据区,主要包括方法区.虚拟机栈.本地方法栈.堆.程序计数器,该部分内存都是线程隔离的. 然后和其交互的有执行引擎.本地库接口,此部分线程之间是可以共享的. 1. 引用计数算法 给对象添加一个引 ...

  3. NYOJ306 走迷宫(dfs+二分搜索)

    题目描写叙述 http://acm.nyist.net/JudgeOnline/problem.php?pid=306 Dr.Kong设计的机器人卡多非常爱玩.它经常偷偷跑出实验室,在某个游乐场玩之不 ...

  4. 2014年同年CFA考试中哪些CFA资料没有变化?

    从2014年起,美国CFA协会将官方教材.题库.模拟题等CFA资料捆绑在报名费用之中,而以往可以单独选购的纸质版教材也变成了额外购买.这让非常多參加12月的CFA考生产生了借阅6月考生CFA资料的想法 ...

  5. 使用Spire PDF for .NET将HTML转换成PDF文档

    目录 开发环境说明 Spire PDF for .NET (free edition)体验 资源下载 开发环境说明 Microsoft Visual Studio 2013 Ultimate Edit ...

  6. Cisco Packet Tracer的使用(一)

    Cisco Packet Tracer 是由Cisco公司发布的一个辅助学习工具,为学习思科网络课程的初学者去设计.配置.排除网络故障提供了网络模拟环境.用户可以在软件的图形用户界面上直接使用拖曳方法 ...

  7. CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\921bbfc4\ca7cf42\App_Code.fu98jwep.dll”--“拒绝访问。 ”

    在本地开发环境没问题,但是发布到服务器出现:未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Fil ...

  8. powerdesigener 12.5注册机

    下载链接 下载链接 密码:awg9

  9. java基础系列——线程池

    一.线程池的创建 我们可以通过ThreadPoolExecutor来创建一个线程池. public ThreadPoolExecutor(int corePoolSize, int maximumPo ...

  10. WPF之Binding的三种简单写法

    环境 类代码 public class Person:INotifyPropertyChanged { private string name; public string Name { get { ...