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 ...
随机推荐
- C++_enum
C++的enum可以限制成员的类型 //error C2440: “=”: 无法从“int”转换为“color” #include <iostream> using namespace s ...
- jeecms 2012 源码分析(2) 前台栏目页静态化分析
还是要说到web.xml文件 <welcome-file-list> <welcome-file>index.html</welcome-file> <wel ...
- makefile之cmake入门
cmake是一款生成makefile的软件:在生成makefile之前,首先是写一个CMakeLists.txt文件: 以下为典型例子: 先看目录tree, 在test文件夹中有:include目录, ...
- javaWeb项目带红色感叹号问题原因
自建Userlibrary 中库中含有非.jar 文件.
- @AutoWired使用
Spring MVC @autowired的使用: spring MVC使用注解配置方式相对于xml配置方式具有很多优势: 充分利用java的反射机制获取类的结构信息,这些信息可以减少配置的工作!Sp ...
- [转] jQuery 操作 JSON 数据
jquery下json数组的操作用法实例: jquery中操作JSON数组的情况中遍历方法用的比较多,但用添加移除这些好像就不是太多了. 试过json[i].remove(),json.remove( ...
- VC6神迹外挂的DIY
2014年09月05日 ⁄ 综合 ⁄ 共 8724字 ⁄ 字号 小 中 大 ⁄ 评论关闭 (一)外挂一般都能在游戏的界面中按一个热键(比如F12,HOME等),就可以呼出外挂的窗口,然后在里面进行外挂 ...
- Ubuntu14下LAMP环境的安装以及yaf扩展的安装
前段时间在ubuntu下安装了lamp环境,记录一下安装过程方便以后查阅. 安装lamp环境 ① 安装apache sudo apt-get install apache2 系统会弹出如图所示的提示, ...
- poj 3740 Easy Finding 精确匹配
题目链接 dlx的第一题, 真是坎坷..... #include <iostream> #include <vector> #include <cstdio> #i ...
- VS插件
VS插件 背景 前些天去考科目二,感觉经历了一场不是高考却胜似高考的考试(10年前的5分之差, 还是难以释怀)! 一行八人,就我学的时间最少(4天,8人一辆车),教练都觉得我肯定还得再来一次! ...