Party Games UVA - 1610

9 篇文章 0 订阅

题目传送门

题意:输入一个n(n是偶数)个字符串的集合,找一个长度最短的字符串S,使得集合当中恰好有一半的字符串小于等于S,有一半大于等于S,如果有多解的话,输出字典序最小的解。

思路:求出来中间的两个字符串然后贪心即可。

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

#define MAXN 200000
#define MAXE 210
#define INF 10000000
#define MOD 1000000007
#define LL long long
#define pi acos(-1.0)

using namespace std;

int main() {
  std::ios::sync_with_stdio(false);
  int n;
  while (cin >> n && n) {
    vector<string> vec;
    string str;
    for (int i = 0; i < n; ++i) {
      cin >> str;
      vec.push_back(str);
    }
    sort(vec.begin(), vec.end());
    string temp1 = vec[n / 2 - 1];
    string temp2 = vec[n / 2];
    bool flag = false;
    for (int i = 0; i < temp1.length(); ++i) {
      string temp = temp1.substr(0, i);
      string s = temp1.substr(0, i + 1);
      if (s >= temp1 && s <= temp2) {
        flag = true;
        cout << s << endl;
        break;
      } else {
        if (temp1[i] == 'Z') {
          continue;
        } else {
          temp += (char)(temp1[i] + 1);
          if (temp >= temp1 && temp < temp2) {
            cout << temp << endl;
            flag = true;
            break;
          }
        }
      }
    }
    if (!flag) {
      cout << temp1 << endl;
    }
  }
}

/*
4
FRED
SAM
JOE
MARGARET
2
FRED
FREDDIE
2
JOSEPHINE
JERRY
2
LARHONDA
LARSEN
0
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值