UVa:1610 Party Games(字符串处理)
来源:互联网 发布:兼职开淘宝店赚钱吗 编辑:程序博客网 时间:2024/05/17 06:26
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=845&page=show_problem&problem=4485
题意:输入一个n(2
≤n≤1000,n是偶数)个字符串的集合D,找一个长度最短的字符串(不一定在D中出现)S,使得D中恰好一半串小于等于S,另一半串大于S。如果有多解,输出字典序最小的解。例如,对于{JOSEPHINE,JERRY},输出JF;对于{FRED,FREDDIE}输出FRED。(本段摘自《算法竞赛入门经典(第2版)》)
分析:基本想法是是对字符串按字典序排序,然后只需处理中间的两个字符串就可以了。主要是对细节的处理要注意。
代码:
#include <iostream>#include <fstream>#include <cstring>#include <vector>#include <queue>#include <cmath>#include <algorithm>#include <set>#include <string>using namespace std;const int maxn = 1005;int n, l, l1, l2;string s, s1, s2, tmp;string a[maxn];int main(){ while (~scanf("%d", &n), n) { s = ""; for (int i = 0; i < n; ++i) cin >> a[i]; sort(a, a + n); s1 = a[n / 2 - 1]; s2 = a[n / 2]; l1 = s1.size(); l2 = s2.size(); l = min(l1, l2); for (int i = 0; i < l; ++i) if (s1[i] == s2[i]) s += s1[i]; else { tmp = s + (char)(s1[i] + 1); if (tmp == s2) { s += s1[i++]; while (i < l1 - 1 && s1[i] == 'Z') s += s1[i++]; if (i < l1 - 1) s += (char)(s1[i] + 1); else s = s1; } else { if (tmp.size() == l1) s = s1; else s = tmp; } break; } cout << s << '\n'; } return 0;}
0 0
- UVa:1610 Party Games(字符串处理)
- UVa 1610 - Party Games(细节处理)
- uva 1610 Party Games (贪心+STL字符串)
- Uva - 1610 - Party Games
- uva 1610Party Games
- UVA 1610 - Party Games
- UVA 1610 Party Games
- uva 1610 Party Games
- UVA 1610 Party Games
- UVA 1610 Party Games
- [UVa 1610] 聚会游戏(Party Games)
- HUNNU11397:Party Games(字符串处理)
- UVa 1610 Party Games——细节处理
- UVA - 1610 Party Games 贪心
- UVA 1610(p253)----Party Games
- 习题8-2 UVA - 1610 Party Games 聚会游戏 (构造字符串)
- uva 1610 - Party Games(细节上的处理是关键,思路比较简单)
- UVA - 1610 - Party Games(模拟or枚举)
- Wolf and Rabbit(hdu 1222)
- 面试精选逻辑推理题总结
- 线段树维护区间的和和单点更新-HDU1166-敌兵布阵
- (zhuan)路径、描边与填充
- 解析接口和抽象类
- UVa:1610 Party Games(字符串处理)
- CODE COMPLETE 2e Chapter 8&9
- C++builder的文件读写操作总结(3)
- OCiOS开发:音频播放器 AVAudioPlayer
- Android四大组件之Activity复习笔记
- 04-树9. Path in a Heap (25) -- 小堆树-利用其性质
- java中的final关键字所起的作用
- iOS 笔记三: UITextView和视图控制器生命周期(View Controller LifeCycle)
- C++源文件到可执行文件的过程