UVA 1610 Party Game(逼近)

来源:互联网 发布:sql中is not null 编辑:程序博客网 时间:2024/05/29 17:48

题意

给出个数为偶数的字符串数组,寻找一个字典序尽量小的字符串将该数组分割成,前一半小于等于该字符串,并后一半大于其。

分析

只需找到排序后最靠中间的两个字符串,在中间插入即可。

刚开始的想法,是分情况讨论,可是情况有点多,自己讨论了半天都没有A掉。

于是想直接构造一个字符串,通过层层逼近,来获得一个答案。

逼近 — 匹配— 修正。

大胆的探索,错了就改,有点二分的意思。。。

#include <bits/stdc++.h>using namespace std;//层层逼近string judge(string a, string b){    int n = a.size();    int id = 0;    string ans = "";    ans += 'A';    while(id < n)    {        while(ans[id] <= 'Z' && ans < a) ++ans[id];//逼近        if(ans[id] <='Z' && ans >= a && ans < b) return ans;//匹配        if(ans[id] != a[id]) --ans[id];//修正        ans += 'A';        id++;    }}string s[1100];int main(){    //freopen("in.txt","r",stdin);    int n;    string a, b;    while(cin >> n && n)    {        for(int i = 0; i < n; i++)            cin >> s[i];        sort(s, s+n);        a = s[n/2-1];        b = s[n/2];        string ans = judge(a,b);        cout << ans << endl;    }    return 0;}
0 0