Party Games UVA - 1610 解题报告

版权声明:大鹏专属 https://blog.csdn.net/NCC__dapeng/article/details/88397345

题目大意:自己写一个串S,使得题中给出串一半小于等于S,另一半大于S,如果有多解,输出字典序,最小的。

思路:一道十分细节的题目,里面的细节点太多,导致反复的改,反复的WA。

刚开始的思路,就是排序,比较中间两个串,但是比较里面的细节非常多,特比是在处理末尾字符的时候,自己最后是卡死在末尾全是Z串上,虽然卡的时间比较长,但感觉收获比较多,不卡题怎么进步呢。

下面给出AC代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1000+100;
const int INF=0x3f3f3f3f;
int n;
string c[maxn];

int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n&&n)
    {
        for(int i=1;i<=n;i++) cin>>c[i];

        int p1=n/2,p2=n/2+1;
        string t1=c[p1],t2=c[p2],s,t;
        int len=max(t1.size(),t2.size());

        for(int i=0;i<len;i++)
        {
            if(t1[i]==t2[i]) s+=t1[i];
            else
            {
                t = s + (char)(t1[i]+1);
                if(t==t2)
                {
                    s+=t1[i];
                    while(i<t1.size()-1&&t1[i]=='Z')
                    {
                        s+=t1[i];
                    }
                    if(i==t1.size()-1) s=t1;
                    else s+=(char)(t1[i]+1);
                }
                else
                {
                    if(t.size()==t1.size()) s=t1;
                    else s=t;
                }
                break;
            }
        }
        cout<<s<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/NCC__dapeng/article/details/88397345