Problem UVA12265-Selling Land

Accept: 309  Submit: 3231
Time Limit: 3000 mSec

Problem Description

Input

There may be multiple test cases in the input. Each test case will begin with an even integer n (2 ≤ n ≤ 1,000) on its own line. On the next n lines will be names, one per line. Each name will be a single word consisting only of capital letters and will be no longer than 30 letters. The input will end with a ‘0’ on its own line.

 Output

For each case, print a single line containing the shortest possible string (with ties broken in favor of the alphabetically smallest) that your host could use to separate her guests. The strings should be printed in all capital letters.
 

 Sample Input

4
FRED
SAM
JOE
MARGARET
2
FRED
FREDDIE
2
JOSEPHINE
JERRY
2
LARHONDA
LARSEN
0
 

 Sample Output

K

FRED

JF

LARI

题解:这个题的细节真的很多,分析之后我采用了如下的方式,首先排序然后取中间两个串这个肯定没啥说的,定义一个ans空串,每次先加入小串的一个字符,如果这个字符小于大串的对应字符,这时答案就能构造出来了,构造方式如下:如果这个字符不是大串的最后一个字符或者这个字符对应的位置(大串-小串)>1,那么如果这个字符不是小串的最后一个字符,ans中该位置的字符++,输出,如果是最后一个,直接输出ans。否则看小串的剩下的字符是否是Z,直到找到一个不是Z的或者到小串的末尾,如果不是Z的字符不是小串的末尾,那么++输出,否则直接输出,如果到了末尾,在后面加一个A输出。这个分支的构造就结束了,如果比到最后也没有找到一个能比出大小的字符,那么就意味着直接输出小串即可。(描述起来太乱,看代码比较好)。

 #include<bits/stdc++.h>

 using namespace std;

 int n;
vector<string> str; int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
while (~scanf("%d", &n) && n) {
str.clear();
string tmp;
for (int i = ; i < n; i++) {
cin >> tmp;
str.push_back(tmp);
} sort(str.begin(), str.end());
string lmid = str[n / - ], rmid = str[n / ];
int llen = lmid.length(), rlen = rmid.length(); bool ok = false;
int i = ;
string ans = "";
while (i < llen && i < rlen) {
ans += lmid[i++];
if (lmid[i - ] < rmid[i - ]) {
if (i != rlen || rmid[i - ] - lmid[i - ] != ) {
if(i != llen) ans[i - ]++;
cout << ans << endl;
ok = true;
break;
}
else {
while (i < llen) {
ans += lmid[i++];
if (ans[i - ] != 'Z') {
if (i != llen)ans[i - ]++;
cout << ans << endl;
ok = true;
break;
}
} if (!ok) {
if(i != llen) ans += 'A';
cout <<ans << endl;
ok = true;
}
break;
}
}
} if (!ok) {
cout << lmid << endl;
}
}
return ;
}

UVA12265-Selling Land(细节处理)的更多相关文章

  1. uva12265 Selling Land

    见紫书.(c,h)的更新策略://前面的高度为0了,直接插入因为ans==-c+h,c大,h还小,那么肯定不是最优左上角,更新新加入列的列//新的一列高度最小,就删掉了其他的,只留这个高度从上到下,从 ...

  2. UVa 12265 (单调栈) Selling Land

    紫书上分析了很多很多,超详细,= ̄ω ̄= 每扫描一行可以计算一个height数组,表示从这块空地向上延伸多少块空地,而且这个数组可以逐行递推. 首先对于每一行来说维护一个单调栈,栈里放的是矩形的左上角 ...

  3. UVa 12265 - Selling Land

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. NOIP2018 - 暑期博客整理

    暑假写的一些博客复习一遍.顺便再写一遍或者以现在的角度补充一点东西. 盛暑七月 初涉基环外向树dp&&bzoj1040: [ZJOI2008]骑士 比较经典的基环外向树dp.可以借鉴的 ...

  5. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  6. Selling Souvenirs CodeForces - 808E (分类排序后DP+贪心)

    E. Selling Souvenirs time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  7. 1549: Navigition Problem (几何计算+模拟 细节较多)

    1549: Navigition Problem Submit Page    Summary    Time Limit: 1 Sec     Memory Limit: 256 Mb     Su ...

  8. Land of Farms HDU - 5556 二分图匹配

    Farmer John and his brothers have found a new land. They are so excited and decide to build new farm ...

  9. Vue.js 和 MVVM 小细节

    MVVM 是Model-View-ViewModel 的缩写,它是一种基于前端开发的架构模式,其核心是提供对View 和 ViewModel 的双向数据绑定,这使得ViewModel 的状态改变可以自 ...

随机推荐

  1. 3. mysql性能分析

    一.mysql query optimizer 1. mysql 中有专门负责优化 select 语句的优化器模块,主要功能:通过计算分析系统中收集的统计信息,为客户端的 Query 提供他认为最优的 ...

  2. Java8 方法引用

    概述 方法引用是用来直接访问类或实例阴茎存在的方法或者构造方法.它需要由兼容的函数式接口(lambda表达式中用到的接口)构成的目标类型上下文. 有时候, 当我们想要实现一个函数式接口的方法, 但是已 ...

  3. Excel通用类工具(一)

    前言 最近项目中遇到要将MySQL数据库中的某些数据导出为Excel格式保存,在以前也写过这样的功能,这次就准备用以前的代码,但是看了一下,这次却不一样,因为在以前用到的都是导出一种或几种数据,种类不 ...

  4. linux服务器重启指令

    一.Linux 的五个重启命令 1.shutdown 2.poweroff 3.init 4.reboot 5.halt 二.五个重启命令的具体说明 shutdown reboot 在linux下一些 ...

  5. JS ES6中的箭头函数(Arrow Functions)使用

    转载这篇ES6的箭头函数方便自己查阅. ES6可以使用“箭头”(=>)定义函数,注意是函数,不要使用这种方式定义类(构造器). 一.语法 基础语法 (参数1, 参数2, …, 参数N) => ...

  6. CSS3 - @keyframes

    语法 @keyframes animationname { keyframes-selector {css-styles;} } 值 描述 animationname 必需.定义动画的名称. keyf ...

  7. SpringBoot项目打war包部署Tomcat教程

    一.简介 正常来说SpringBoot项目就直接用jar包来启动,使用它内部的tomcat实现微服务,但有些时候可能有部署到外部tomcat的需求,本教程就讲解一下如何操作 二.修改pom.xml 将 ...

  8. concrrent类下 BlockingDeque 下 自己实现代码编写

    一.LinkedBlockingDeque简介 java6增加了两种容器类型,Deque和BlockingDeque,它们分别对Queue和BlockingQueue进行了扩展. Deque是一个双端 ...

  9. DOM对象和window对象

    本文内容: DOM对象 Window 对象 首发日期:2018-05-11 DOM对象: DOM对象主要指代网页内的标签[包括整个网页] 比如:document代表整个 HTML 文档,用来访问页面中 ...

  10. (python)数据结构---集合

    一.描述 set翻译为集合 set是可变的.无序的.不可重复的 set的元素要求可哈西(不可变的数据类型可哈西,可变的数据类型不可哈希) set是无序的,因此不可以索引,也不可以修改 线型结构的查询时 ...