(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. 第五章 Spring3.0 、Hibernate3.3与Struts2的整合 基于Annotation

    Annotation的方式是通过注解的方式把Struts2中的Action.Dao层的实现类.Service层的实现类交由Spring管理,不需要在配置文件中进行配置.但为了方便,事务的管理依然使用的 ...

  2. Unix/Linux环境C编程入门教程(5) Red Hat Enterprise Linux(RHEL)环境搭建

    Unix/Linux版本众多,我们推荐Unix/Linux初学者选用几款典型的Unix/Linux操作系统进行学习. 通过./a.out ./Y.out执行出结果,证明C++程序编译成功,也就说明li ...

  3. 字符编码和python使用encode,decode转换utf-8, gbk, gb2312

    ASCII码 标准ASCII码使用7位二进制数表示大写或小写字母,数字0到9标点符号以及在美式英语中使用的特殊控制字符. 在标准ASCII码中,最高位(b7)用作奇偶校验位,所谓奇偶校验,是指在代码传 ...

  4. 起启航-华夏互联与杰华网络合体结盟打造本土IT利舰

    北京时间2013年9月9日消息: 领先的软件研发企业上海逐一软件科技有限公司与专业互联网推广运营机构南昌杰华网络开发有限公司达成协议,双方将建立紧密合作关系与集团运营体制,并在未来的10个月内进行相应 ...

  5. 能够免费做商业站点的CMS讨论

    眼下国内使用过PHPCMS   DEDECMS织梦    科讯CMS  帝国.Discuz.Ecshop等,可是他们都是个人非盈利免费,商业.政府.机构授权收费. 使用什么CMS能够免费做商业站点呢? ...

  6. C#高级编程三十天----泛型结构,泛型方法,泛型托付

    泛型结构 泛型结构和泛型类差点儿是一直的,仅仅是泛型结构没有继承的特性..NET平台提供的一个泛型结构是(可空类型)Nullablle<T>.可空类型的引入,主要是为了解决数据库语言中的数 ...

  7. win8.1 安装webaccess遇到的写访问权限错误

    大学的时候闲暇时间都用来玩游戏了,现在工作了,就把工作中遇到的问题和学习心得跟大家共享下,若有不对的地方欢迎指出,谢谢! 前几天突然想换用vs2013开发,就重装了win8.1系统,在新系统上安装we ...

  8. SQL Server索引进阶:第十五级,索引的最佳实践

    在本文中我们将推荐14条贯穿本系列的规则,这些规则帮助你为数据库创建最好的索引结构. 格式来自于<Framework Design Guidelines>.每条推荐用四个词来总结:Do做, ...

  9. 编写可维护的JS 03

    3.语句和表达式 所有语句都应当使用花括号 if else语句 for 循环 while 循环 do...while try...catch...finally 花括号对齐方式 左括号在第一行语句末尾 ...

  10. C#后台创建控件并获取值

    前台代码 <form id="form1" runat="server"> <div> <div class="item ...