• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
dwtfukgv
博客园    首页    新随笔    联系   管理    订阅  订阅
UVa 1610 Party Games(思维)

题意: 给出一系列字符串,构造出一个最短字符串(可以不在集合中)大于等于其中的一半,小于另一半。

析:首先找出中间的两个字符串,然后暴力找出最短的字符串,满足题意。

代码如下:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>

using namespace std;
vector<string> v;

int main(){
//    freopen("in.txt", "r", stdin);
    int n;
    while(scanf("%d", &n) && n){
        v.clear();
        string s;
        for(int i = 0; i < n; ++i){
            cin >> s;
            v.push_back(s);
        }

        sort(v.begin(), v.end());
        int len = v.size();
        string s1 = v[len/2 - 1];
        string s2 = v[len/2];

        len = s1.size();
        int p = 0;
        string ans = "A";
        while(p < len){
            while(ans[p] <= 'Z' && ans < s1)  ++ans[p];
            if(ans[p] <= 'Z' && ans >= s1 && ans < s2)  break;
            if(ans[p] != s1[p]) --ans[p];
            ans += 'A';
            ++p;
        }
        cout << ans << endl;
    }
    return 0;
}

 

posted on 2016-05-22 19:10  dwtfukgv  阅读(467)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3