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 ...
随机推荐
- flex4 日期类型字符串转日期类型(string转Date)(转)
mysql数据库中存储的日期类型通过PHP返回到flex端为字符串类型,这样在flex中进行处理时就必须要将字符串转化为Date类型.如果仅仅是 "年/月/日" 的组合,而没有涉及 ...
- c++ new长度为0的数组
在程序中发现一下代码: int CHmcVideoMgt ::OnGetDiskRunningInfo( SOCKETPARAM *pSocketInfo ,Json:: Value Param ) ...
- Oracle_用户管理
创建用户: CREATE USER user --创建用户user IDENTIFIED {BY password | EXTERNALLY} --设备用户密码,EXTERNALLY说用该用户由 ...
- C++中实现链表的删除和颠倒
MFC工程中关于链表的操作 1.对于给定的整数n,编写算法删除链表中第n个节点,该链表的第一个节点由first指向. 由于C++中没有关于node的标准头文件,要先手动定义node类,此处只定义了简单 ...
- overfllow的解析
参数是scroll时候,必会出现滚动条.参数是auto时候,子元素内容大于父元素时出现滚动条.参数是visible时候,溢出的内容出现在父元素之外.参数是hidden时候,溢出隐藏.
- hive集群安装配置
hive 是JAVA写的的一个数据仓库,依赖hadoop.没有安装hadoop的,请参考http://blog.csdn.net/lovemelovemycode/article/details/91 ...
- uva10617 - Again Palindrome(dp)
再次回文 输入:标准输入 输出:标准输出 时间限制: 2秒 是àpalindorme的读取相同的从左边,因为它从右侧的一个或多个字符的序列.例如,Ž,TOT和女士的 回文,但是,ADAM是不是. 给定 ...
- 一个SQL update语句
须要每隔一段时间选取最老的商户更新时间戳: update DP_Shop set DP_Shop.LastDate = now() where DP_Shop.ShopId in (select Sh ...
- golang之interface(接口)与 reflect 机制
一.概述 什么是interface,简单的说,interface是一组method的组合,通过interface来定义对象的一组行为: interface类型定义了一组方法,如果某个对象实现了某个接口 ...
- ASP.NET页面上传文件时提示文件大小超过请求解决方法
在webconfig中节点 <system.web> </system.web> 下加入以下代码:maxRequestLength为限制上传文件大小,executionTime ...