Codeforces Round #455 (Div. 2) A. Generate Login【贪心】

时间:2023-02-07 14:12:02
A. Generate Login
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

The preferred way to generate user login in Polygon is to concatenate a prefix of the user's first name and a prefix of their last name, in that order. Each prefix must be non-empty, and any of the prefixes can be the full name. Typically there are multiple possible logins for each person.

You are given the first and the last name of a user. Return the alphabetically earliest login they can get (regardless of other potential Polygon users).

As a reminder, a prefix of a string s is its substring which occurs at the beginning of s: "a", "ab", "abc" etc. are prefixes of string "{abcdef}" but "b" and 'bc" are not. A string a is alphabetically earlier than a string b, if a is a prefix of b, or a and b coincide up to some position, and then a has a letter that is alphabetically earlier than the corresponding letter in b: "a" and "ab" are alphabetically earlier than "ac" but "b" and "ba" are alphabetically later than "ac".

Input

The input consists of a single line containing two space-separated strings: the first and the last names. Each character of each string is a lowercase English letter. The length of each string is between 1 and 10, inclusive.

Output

Output a single string — alphabetically earliest possible login formed from these names. The output should be given in lowercase as well.

Examples
input
harry potter
output
hap
input
tom riddle
output
tomr

【题意】:题意是求两个字符串的缩写,具体规则就是两个字符串,每个字符串的缩写可以是0 ~ 字符串长度的任意长,然后合并起来,求字母顺序最小的前缀。

【分析】:substr用于获得子串。

最直接的解决方案是生成所有可能的登录(通过尝试所有非首字母和姓氏的前缀,并将它们组合),并按字母顺序查找最早的字母。为了获得更快的解决方案,需要进行一些观察。首先,在字母最早的登录名中,姓的前缀总是一个字母; 无论使用两个或多个姓氏字母生成的登录信息,都可以通过删除多余的字母来进一步缩短字母以获得更早的登录信息。其次,名字的前缀不能包含任何大于或等于姓的第一个字母的字母,而不是第一个字母。因此,更好的解决方案是:从第二个字母开始迭代第一个字母的字母。一旦找到大于或等于姓氏第一个字母的字母,停止,并返回所有字母,直到这个字母加上姓氏的第一个字母。如果没有找到这样的信件,则返回整个名字加上姓氏的第一个字母。

【代码】:

#include <bits/stdc++.h>

using namespace std;
string s,ss,s1="zzzzzzzzzz";
int main()
{
cin>>s>>ss;
for(int i=;i<s.size();i++)
{
for(int j=;j<ss.size();j++)
{
s1=min(s1,s.substr(,i+)+ss.substr(,j+));
}
}
cout<<s1<<endl;
return ;
}

贪心

Codeforces Round #455 (Div. 2) A. Generate Login【贪心】的更多相关文章

  1. Codeforces Round &num;455 &lpar;Div&period; 2&rpar;

    Codeforces Round #455 (Div. 2) A. Generate Login 题目描述:给出两个字符串,分别取字符串的某个前缀,使得两个前缀连起来的字符串的字典序在所有方案中最小, ...

  2. 【Codeforces Round &num;455 &lpar;Div&period; 2&rpar; A】Generate Login

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举两个串的前缀长度就好. 组出来. 排序. 取字典序最小的那个. [代码] #include <bits/stdc++.h& ...

  3. Codeforces Round &num;455 &lpar;Div&period; 2&rpar; 909D&period; Colorful Points

    题 OvO http://codeforces.com/contest/909/problem/D CF 455 div2 D CF 909D 解 算出模拟的复杂度之后就是一个很水的模拟题 把字符串按 ...

  4. Codeforces Round &num;455 &lpar;Div&period; 2&rpar; 909E&period; Coprocessor

    题 OvO http://codeforces.com/contest/909/problem/E CF455 div2 E CF 909E 解 类似于拓扑排序地进行贪心, 对于 Ei=0 并且入度为 ...

  5. 【Codeforces Round &num;455 &lpar;Div&period; 2&rpar; B】Segments

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 处理出所有的线 其实就是区间. 总共有n*(n+1)/2个 然后按照左端点.右端点排序 每次取最左边的线. 多种可能就取右端点尽量小 ...

  6. 【Codeforces Round &num;455 &lpar;Div&period; 2&rpar; C】 Python Indentation

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 一个for循环之后. 下一个写代码的地方一是从(x+1,y+1)开始的 然后如果写完了一个simple statement 下次就有 ...

  7. Codeforces Round &num;455 &lpar;Div&period; 2&rpar; D题&lpar;花了一个早自习补了昨晚的一道模拟QAQ&rpar;

    D. Colorful Points You are given a set of points on a straight line. Each point has a color assigned ...

  8. Codeforces Round &num;180 &lpar;Div&period; 2&rpar; D&period; Fish Weight 贪心

    D. Fish Weight 题目连接: http://www.codeforces.com/contest/298/problem/D Description It is known that th ...

  9. Codeforces Round &num;180 &lpar;Div&period; 2&rpar; A&period; Snow Footprints 贪心

    A. Snow Footprints 题目连接: http://www.codeforces.com/contest/298/problem/A Description There is a stra ...

随机推荐

  1. 实现跨域的N种方法

    从域说起 域: 域是WIN2K网络系统的安全性边界.我们知道一个计算机网最基本的单元就是"域",这一点不是WIN2K所独有的,但活动目录可以贯穿一个或多个域.在独立的计算机上,域即 ...

  2. &lpar;一&rpar;win7下cocos2d-x 21 &plus; vs2010

    1.下载SDK http://cocos2d.cocoachina.com/download,我下载2.1版本,cocos2d-2.1rc0-x-2.1.2-hotfix.zip @ Apr.08, ...

  3. 九度OJ 1077 最大序列和 -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1077 题目描述: 给出一个整数序列S,其中有N个数,定义其中一个非空连续子序列T中所有数的和为T的“序列和”. 对 ...

  4. c&num;基础语句——循环语句(for、while、foreach)

    循环类型:for.while.foreach 循环四要素:初始条件-->循环条件-->循环体-->状态改变 1.for 格式: for(初始条件:循环条件:状态改变) {循环体(br ...

  5. express学习之session

    最新版本的express没有包含session依赖项,需要我们自己配置并安装. 安装方法:npm install express-session 使用:var session = require('e ...

  6. 你必须知道的 SmartSql !

    介绍 SmartSql = MyBatis + Cache(Memory | Redis) + R/W Splitting +Dynamic Repository + Diagnostics .... ...

  7. STL中erase&lpar;&rpar;的用法

    erase()是STL提供的容器中比较常用的方法之一,它的功能是删除容器中的某些元素,其中它的函数原型如下: 1.有两个参数,且参数类型都是size_t型: string& erase ( s ...

  8. Python学习一&vert;anaconda的安装问题以及Python语言的特点

    安装时遇到的问题 安装anaconda3.0到D盘之后,配置好两个环境变量:D:\anaconda和D:\anaconda\Scripts.发现在命令行中执行python指令可以,但conda指令却是 ...

  9. 838&period; Push Dominoes

    There are N dominoes in a line, and we place each domino vertically upright. In the beginning, we si ...

  10. ACdream1092

    题意是给出某个地鼠的出现位置以及出现时间,人有一个移动速度,求此人最多可以打多少个地鼠? 我们根据时间把所有的地鼠排序,如果两个地鼠之间的距离不超过时间只差与速度的乘积,那说明打完上一只地鼠还可以打到 ...