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
Sample Input
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(细节处理)的更多相关文章
- uva12265 Selling Land
见紫书.(c,h)的更新策略://前面的高度为0了,直接插入因为ans==-c+h,c大,h还小,那么肯定不是最优左上角,更新新加入列的列//新的一列高度最小,就删掉了其他的,只留这个高度从上到下,从 ...
- UVa 12265 (单调栈) Selling Land
紫书上分析了很多很多,超详细,= ̄ω ̄= 每扫描一行可以计算一个height数组,表示从这块空地向上延伸多少块空地,而且这个数组可以逐行递推. 首先对于每一行来说维护一个单调栈,栈里放的是矩形的左上角 ...
- UVa 12265 - Selling Land
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- NOIP2018 - 暑期博客整理
暑假写的一些博客复习一遍.顺便再写一遍或者以现在的角度补充一点东西. 盛暑七月 初涉基环外向树dp&&bzoj1040: [ZJOI2008]骑士 比较经典的基环外向树dp.可以借鉴的 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- Selling Souvenirs CodeForces - 808E (分类排序后DP+贪心)
E. Selling Souvenirs time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 1549: Navigition Problem (几何计算+模拟 细节较多)
1549: Navigition Problem Submit Page Summary Time Limit: 1 Sec Memory Limit: 256 Mb Su ...
- 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 ...
- Vue.js 和 MVVM 小细节
MVVM 是Model-View-ViewModel 的缩写,它是一种基于前端开发的架构模式,其核心是提供对View 和 ViewModel 的双向数据绑定,这使得ViewModel 的状态改变可以自 ...
随机推荐
- python中的eval函数
eval() 函数十分强大 -- 将字符串 当成 有效的表达式 来求值 并 返回计算结果 In [1]: eval("1 + 3") Out[1]: 4 In [2]: eval( ...
- JDK动态代理简单使用(2)
JDK动态代理使用: 使用JDK动态代理步骤: ①创建被代理的接口和类: public interface IA { void f1(String param); } public class A i ...
- 前端入门2-HTML标签
本篇文章已授权微信公众号 dasu_Android(大苏)独家发布 声明 本系列文章内容全部梳理自以下四个来源: <HTML5权威指南> <JavaScript权威指南> MD ...
- mac node版本管理
(0)简说 目前有n和nvm这两个工具可以对Node进行升级,以下简单介绍一下二者的使用. (1)n 安装很简单: $ sudo npm install -g n 另一种获取源码的方法安装: $ gi ...
- 自定义mvc或mtv框架:基于wsgiref的web框架
把mvc或mtv框架的model数据库,view:html,control逻辑处理,url判别,wsgiref集中在一个文件 代码如下 #!/usr/bin/env python #-*- codin ...
- IO学习二(节点流)
1.流的分类 按照数据流向的不同:输入流和输出流 按照处理数据的单位不同:字节流((非文本文件)视频.音频.图像).字符流(文本文件) 按照角色的不同:节点流和处理流 2.IO体系 抽象基类 节点流 ...
- 关于购物车添加按钮的动画(vue.js)
来自:https://segmentfault.com/a/1190000009294321 (侵删) git 源码地址 https://github.com/ustbhuangyi/vue-sel ...
- JS之console.log详解以及兄弟姐们邻居方法扩展
console.log() 基本用法 console.log,前端常用它来调试分析代码,你可以在任何的js代码中调用console.log(),然后你就可以在浏览器控制台看到你刚才打印的常量,变量,数 ...
- Mixing a dll boost library with a static runtime is a really bad idea错误的解决
作者:朱金灿 来源:http://blog.csdn.net/clever101 同事在使用boost库时遇到一个问题,在编译时出现一个错误:Mixing a dll boost library wi ...
- verilog实现红黄蓝三秒灯
代码如下 test.v文件 led.v文件 module test(); wire led_r,led_g,led_b; ; clk <= ~clk; led c1 ( .clk(clk), . ...