Codeforces Round #455 (Div. 2) A. Generate Login【贪心】
2 seconds
256 megabytes
standard input
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".
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 a single string — alphabetically earliest possible login formed from these names. The output should be given in lowercase as well.
harry potter
hap
tom riddle
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【贪心】的更多相关文章
- Codeforces Round #455 (Div. 2)
Codeforces Round #455 (Div. 2) A. Generate Login 题目描述:给出两个字符串,分别取字符串的某个前缀,使得两个前缀连起来的字符串的字典序在所有方案中最小, ...
- 【Codeforces Round #455 (Div. 2) A】Generate Login
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举两个串的前缀长度就好. 组出来. 排序. 取字典序最小的那个. [代码] #include <bits/stdc++.h& ...
- Codeforces Round #455 (Div. 2) 909D. Colorful Points
题 OvO http://codeforces.com/contest/909/problem/D CF 455 div2 D CF 909D 解 算出模拟的复杂度之后就是一个很水的模拟题 把字符串按 ...
- Codeforces Round #455 (Div. 2) 909E. Coprocessor
题 OvO http://codeforces.com/contest/909/problem/E CF455 div2 E CF 909E 解 类似于拓扑排序地进行贪心, 对于 Ei=0 并且入度为 ...
- 【Codeforces Round #455 (Div. 2) B】Segments
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 处理出所有的线 其实就是区间. 总共有n*(n+1)/2个 然后按照左端点.右端点排序 每次取最左边的线. 多种可能就取右端点尽量小 ...
- 【Codeforces Round #455 (Div. 2) C】 Python Indentation
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 一个for循环之后. 下一个写代码的地方一是从(x+1,y+1)开始的 然后如果写完了一个simple statement 下次就有 ...
- Codeforces Round #455 (Div. 2) D题(花了一个早自习补了昨晚的一道模拟QAQ)
D. Colorful Points You are given a set of points on a straight line. Each point has a color assigned ...
- Codeforces Round #180 (Div. 2) D. Fish Weight 贪心
D. Fish Weight 题目连接: http://www.codeforces.com/contest/298/problem/D Description It is known that th ...
- Codeforces Round #180 (Div. 2) A. Snow Footprints 贪心
A. Snow Footprints 题目连接: http://www.codeforces.com/contest/298/problem/A Description There is a stra ...
随机推荐
- input type=file输入框
<div class="row"> <!--选择图片按钮--> <div class="col-xs-12" align=&quo ...
- 怎么把myeclipse项目导入IDEA中
先把myeclipse下的项目拷贝到IDEA的部署目录中,把一些不用的配置文件删除,只留下一个干净的项目 打开IDEA,点击import Project,引入一个项目,选择IDEA部署目录下刚拷贝过去 ...
- Proguard returned with error code 1. See console
满世界的bug. 微信支付,Windows的远程桌面. Android的 , Proguard returned with error code 1. See console解决办法" 真的 ...
- Redis实现之对象(一)
对象 前面我们介绍了Redis的主要数据结构,如:简单动态字符串SDS.双端链表.字典.压缩列表.整数集合等.Redis并没有直接使用这些数据结构来实现键值对数据库,而是基于这些数据结构创建了一个对象 ...
- TCP/IP网络编程之地址族与数据序列
分配IP地址和端口号 IP是Internet Protocol(网络协议)的简写,是为收发网络数据而分配给计算机的值.端口号并非赋予计算机的值,而是为区分程序中创建的套接字而分配给套接字的序号 网络地 ...
- Java中为什么字段不能被重写
官方说法: 在一个类中,一个具有相同名称的字段隐藏了父类的父类的领域,即使他们的类型是不同的.在子类中,父类中的字段是不能用简单的名称引用.相反,该字段必须通过超级访问.一般来说,我们不建议隐藏字段, ...
- 设计模式之第3章-模板方法模式(Java实现)
设计模式之第3章-模板方法模式(Java实现) "那个,上次由于我老婆要给我做饭,所以就没有说完就走掉了...这个那个".这次和以前一样,先来开场福利(工厂方法模式已被作者踹下场) ...
- python 笔试总结
1.对比两种函数对应结果 def fn(x): if x>0: print(x) fn(x-1) ****结果****** 3 2 1 $$$$$$另外一种$$$$$$$$$ def fn(x) ...
- springcloud 高可用分布式配置中心
SpringCloud教程七:高可用的分布式配置中心(SpringCloud Config) 当服务有很多 都要从服务中心获取配置时 这是可以将服务中心分布式处理 是系统具备在集群下的大数据处理 主要 ...
- JavaWeb笔记(一)JDBC
基本步骤 导入MySQL驱动jar包 mysql-connector-java-8.0.15.zip 注册驱动 获取数据库连接对象Connection 定义sql 获取执行sql语句的对象Statem ...