Studious Student Problem Analysis
(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的更多相关文章
- 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 ...
- 903A. Hungry Student Problem#饥饿的学生(暴力&双层枚举)
题目出处:http://codeforces.com/problemset/problem/903/A 题目大意就是:一个数能否用正整数个另外两个数合成 #include<iostream> ...
- 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 ...
- [转]NLP Tasks
Natural Language Processing Tasks and Selected References I've been working on several natural langu ...
- cvpr2015papers
@http://www-cs-faculty.stanford.edu/people/karpathy/cvpr2015papers/ CVPR 2015 papers (in nicer forma ...
- Atitit 编程语言编程方法的进化演进 sp COP ,AOP ,SOP
Atitit 编程语言编程方法的进化演进 sp COP ,AOP ,SOP 1.1. Sp oop>>COP ,AOP ,SOP1 1.2. Sp oop 结构化方法SP(Stru ...
- leetcode 111 minimum depth of binary tree
problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...
- 发布一个免费开源软件-- PAD流程图绘制软件PADFlowChart
软件的可执行文件下载:PADFlowChart-exe.zip MD5校验码:91FCA9FEC9665FD09BEB3DA94ADC1CE6 SHA1校验码:ECD742AA3092A085AB07 ...
- 北大poj-1081
You Who? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 801 Accepted: 273 Descriptio ...
随机推荐
- [Drools]JAVA规则引擎 -- Drools
Drools是一个基于Java的规则引擎,开源的,可以将复杂多变的规则从硬编码中解放出来,以规则脚本的形式存放在文件中,使得规则的变更不需要修正代码重启机器就可以立即在线上环境生效. 本文所使用的de ...
- 转:onkeypress、onkeydown、onkeyup 区别
在使用JavaScript做WEB键盘事件侦听捕获时,主要采用onkeypress.onkeydown.onkeyup三个事件进行出来.该三个事件的执行顺序如下:onkeydown -> onk ...
- html中文乱码
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">改成<m ...
- 2014 Web开发趋势
本文翻译自:http://www.pixelstech.net/article/1401629232-Web-design-trends-for-2014 如今,已然到了Web横行的时代.越来越多的资 ...
- 在.NET中快速创建一个5GB、10GB或更大的空文件
对于通过UDP进行打文件传输的朋友应该首先会考虑到一个问题,那就是由于UDP并不会根据先来先到原则进行发送,也许你发送端发送的时候是以包1和包2的顺序传输的,但接收端可能以包2和包1 的顺序来进行接收 ...
- 不要再坑人啦!NSOperation才是苹果推荐使用的多线程技术!
首先声明一下.小汤我在实际开发中呢,实际上也是喜欢用GCD多一点,由于用起来感觉更方便. 可是,事实是什么呢? 苹果推荐使用的实际上是NSOperation.所以希望对此没有研究的各位不要再在博客中误 ...
- SQLSERVER常用脚本整理
数据库存储空间查询(数据库的大小及数据库中各个表的数据量和每行记录大小) IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = Object_i ...
- 母版页中ContentPlaceHolder 控件的作用
文章转自 http://wenku.baidu.com/link?url=7sLN0ihgZQ1XfX47b_y8qbpIVjS5T75Q1xvaoyIQ6OiKIgvzyVyRccnU9e9fqo ...
- java中关于如何运行jar格式程序的说明
通常情况下,我们用打包工具如Eclipse的export工具制作的jar包是无法通过鼠标双击来运行的. 此时我们需要启动DOS窗体,在DOS窗体中输入java命令运行程序(前提是你的环境变量class ...
- android入门——UI(4)
GridView控件实现菜单 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xml ...