CF909A Generate Login (贪心)(来自洛谷)

洛谷地址:https://www.luogu.com.cn/problem/CF909A

来自洛谷的题意:

给定两个用空格分隔的字符串,分别取两字符串的任意非空前缀,将两前缀合并为一个新的字符串,求可行字典序最小的字符串。

解析:

给定s1 s2

已知取两者非空前缀,所以s2只需要取首就可以了。

那么s1[0]和s2[0]是必取的

举个例子,有az和abz,谁的字典序小?很明显:abz

所以对于s1[0]....s2[0]

中间插入字符的条件是<s2[0]

#include<cstdio>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
priority_queue<int,vector<int>,greater<int> > q;//优先为小的优先队列 
typedef long long ll;
const int maxn=5e3+20;
ll a[maxn];
struct node
{
    int x,id;
}st[maxn];
bool cmp(node a , node b)
{
    if(a.x==b.x)
        return a.id<b.id;
    return a.x<b.x;
}
int main()
{    // 4 0 20
    string s1,s2;
    cin>>s1;
    cin>>s2;
    string md;
    md+=s1[0];
    for(int i=1;i<s1.length();i++)
    {
        if(s1[i]<s2[0])
            md+=s1[i];
        else
            break;
    }
    cout<<md<<s2[0]<<endl;
}

 

posted @ 2020-07-09 23:40  liyexin  阅读(156)  评论(0编辑  收藏  举报