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. 6.4 group by 优化

    1.小总结 group by 实质是先排序后进行分组,遵照索引建的最佳左前缀. 当无法使用索引列,增大max_length_for_sort_data参数的设置 + 增大sort_buffer_siz ...

  2. java使用POI将数据导出放入Excel

    本文主要是将数据库取出的数据按照自定义的行列格式导出到excel中,POI则是实现我们需求所用到的技术. POI介绍 使用spring boot导入相关依赖 获取数据(自行处理) 完整代码实例:创建e ...

  3. 在CentOS下面安装hue时报的错

    说明:我的系统为CentOS 7 ,系统自带的python版本为2.7.5. 安装hue时,推荐使用2.7.0以上的版本,可以自己查看自己系统自带的版本 若是版本不对,要升级为2.7的版本,这里不再说 ...

  4. react学习(一)

    组件和属性(props) 函数式组件: function Welcome(props) { return <h1>Hello, {props.name}</h1>; } 渲染一 ...

  5. CSS层叠样式表(Cascading Style sheets)

    CSS层叠样式表(Cascading Style sheets) --------- ---------------- ----------- --------------- ----------- ...

  6. Sql 判断函数是否存在、sql判断表是否存在、sql判断存储过程是否存在、sql判断视图是否存在

    --数据库是否存在 IF exists(SELECT * FROM master..sysdatabases WHERE name=N'库名') PRINT 'exists' ELSE PRINT ' ...

  7. IE CSS Hack【记录】

    1.条件hack 2.属性hack 3.选择器hack CSS Hack一般都是利用各浏览器的支持CSS的能力和BUG来进行的 本文只列举了一些常用的CSS Hack,且不考虑IE6以下的版本 尽可能 ...

  8. loj#2483. 「CEOI2017」Building Bridges(dp cdq 凸包)

    题意 题目链接 Sol \[f[i], f[j] + (h[i] - h[j])^2 + (w[i - 1] - w[j]))\] 然后直接套路斜率优化,发现\(k, x\)都不单调 写个cdq就过了 ...

  9. Python-Django 第一个Django app

    第一个Django app   by:授客 QQ:1033553122 测试环境: Python版本:python-3.4.0.amd64 下载地址:https://www.python.org/do ...

  10. matlab练习程序(求向量间的旋转矩阵与四元数)

    问题是这样,如果我们知道两个向量v1和v2,计算从v1转到v2的旋转矩阵和四元数,由于旋转矩阵和四元数可以互转,所以我们先计算四元数. 我们可以认为v1绕着向量u旋转θ​角度到v2,u垂直于v1-v2 ...