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;
}
原文地址:https://www.cnblogs.com/dwtfukgv/p/5517482.html