(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. Solr学习(2) Solr4.2.0+IK Analyzer 2012

    Solr学习(二) Solr4.2.0+IK Analyzer 2012 开场白: 本章简单讲述如何在solr中配置著名的 IK Analyzer 分词器. 本章建立在 Solr学习(一)  基础上进 ...

  2. Quality Over Quantity: 更少一些,更好一些_第1页_福布斯中文网

    Quality Over Quantity: 更少一些,更好一些_第1页_福布斯中文网     Quality Over Quantity: 更少一些,更好一些    2013年04月09日     ...

  3. _.each的用法2

    有这样一个需求:一个对象数组: 内容如下: {[ {id:"ssss",position:"4"}, {id:"ssss",position ...

  4. mac 版本号控制工具SmartSVN7.5.4(破解版)

    SmartSVN7.5.4和破解工具,下载地址:http://download.csdn.net/detail/pearlhuzhu/7407319 操作步骤: 1.在MAC上选中smartsvn-m ...

  5. xcode KVC:Key Value Coding 键值编码

    赋值 // 能修改私有成员变量 - (void)setValue:(id)value forKey:(NSString *)key; - (void)setValue:(id)value forKey ...

  6. ASP.NET页面上传文件时提示文件大小超过请求解决方法

    在webconfig中节点 <system.web> </system.web> 下加入以下代码:maxRequestLength为限制上传文件大小,executionTime ...

  7. 网页平面设计 CSS

    1.在html中引入css的方法 1.行内式 行内式即在标记的style属性中设定CSS样式,这种方式本质上没有体现出CSS的优势,因此不推荐使用. 例如:<h1 style="属性名 ...

  8. java调试工具

    jps当前用户已启动的java进程信息,信息包括进程号和简短的进程command. jstat输出指定 jvm 实例的特定统计量:统计量:-class-compiler-gc-gccapacity-g ...

  9. js apply 和call的区别

    function Person(name, profession) { this.name = name; this.profession = profession; this.speak = fun ...

  10. 任务栏流量监测工具 NetSpeedMonitor 在Windows 8下的安装使用

    这个是给不喜欢360等提供的桌面浮动网络监控的园友准备的,NetSpeedMonitor 是一个可以在任务栏监控流量的小工具,集成在任务栏上显示,可以手动设置单位.文字大小等.还支持监控日志,相比其他 ...