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. maven插件调试方法

    前言 以IntelliJ IDEA为例,通常我们调试maven项目时,都是直接点击调试按钮即可.但maven编写的插件就不同了,由于插件需要打包成Jar加载到项目中,所以如果我们需要在编写插件源码时调 ...

  2. (8)Microsoft office Word 2013版本操作入门_制作传单海报

    1.纸张大小,方向设定. 1.1纸张大小: [页面布局]----[纸张大小] 可以选择已有的尺寸,也可以选择其他自定义的大小. 1.2 方向设定: [页面布局]--[纸张方向]选择 横向或者纵向 2. ...

  3. Android Studio 学习(六)内容提供器

    运行时权限 使用ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.CALL_PHONE)!=Package ...

  4. 判断文本是否溢出/hover显示全部

    前言 在工作中我们经常会遇到,文字过多,需要用省略号,并且鼠标hover的时候 还需要 显示全部的文字的需求. 正文 文字过多需要用省略号的实现:上代码啦 .ellipsis { width: 100 ...

  5. laravel5.5通过Migrations修改表 的artisan命令

    1,不同表的修改都需要通过命令创建一个文件 2,首先通过artisan创建对应表的一个文件 php artisan make:module:migration abtinvitcard(模块名) al ...

  6. 带你使用JS-SDK自定义微信分享效果

    前言 想必各位在写wap端时都遇到过这样的场景吧 ----自定义分享标题.图片.描述 接下来小编给大家讲解下分享相关操作 预期效果 原始的分享效果: 使用微信JS-SDK的分享效果: 可以看出缩略图, ...

  7. gulp解决跨域的配置文件

    //引入插件 var gulp = require('gulp'); // var Proxy = require('gulp-connect-proxy'); var connect = requi ...

  8. express 连接数据库

    (1)创建项目 ,项目名cntMongodb express -e cntMongodbcd cntMonfodbnpm installnpm install mongoose --save //安装 ...

  9. elementUI vue table status的状态列颜色变化和操作列状态显示(停用, 启用)

    <div id="app" style="display: none"> ... <el-table-column prop="st ...

  10. MongoDB 通过配置文件启动及注册服务

    1.配置mongodb环境变量,配置完成之后就可以直接执行mong.mongod等常用命令,不用每次都到mongodb安装目录bin下去执行: 2.通过命令启动mongo服务 mongod --dbp ...