Codeforces Round #550 (Div. 3) E. Median String (模拟)
2 seconds
256 megabytes
standard input
standard output
You are given two strings ss and tt, both consisting of exactly kk lowercase Latin letters, ss is lexicographically less than tt.
Let's consider list of all strings consisting of exactly kk lowercase Latin letters, lexicographically not less than ss and not greater than tt (including ss and tt) in lexicographical order. For example, for k=2k=2, s=s="az" and t=t="bf" the list will be ["az", "ba", "bb", "bc", "bd", "be", "bf"].
Your task is to print the median (the middle element) of this list. For the example above this will be "bc".
It is guaranteed that there is an odd number of strings lexicographically not less than ss and not greater than tt.
The first line of the input contains one integer kk (1≤k≤2⋅1051≤k≤2⋅105) — the length of strings.
The second line of the input contains one string ss consisting of exactly kk lowercase Latin letters.
The third line of the input contains one string tt consisting of exactly kk lowercase Latin letters.
It is guaranteed that ss is lexicographically less than tt.
It is guaranteed that there is an odd number of strings lexicographically not less than ss and not greater than tt.
Print one string consisting exactly of kk lowercase Latin letters — the median (the middle element) of list of strings of length kk lexicographically not less than ss and not greater than tt.
2
az
bf
bc
5
afogk
asdji
alvuw
6
nijfvj
tvqhwp
qoztvz
题意:给定两个长度为n的字符串s和t,它们按照字典序排列有t>s,求字符串按照字典序排列位于s和t中间位置的那个字符串(题目保证在正中间)。比如样例az和bf,它们两个按照字典序排列的字符串集合是【az,ba,bb,bc,bd,be,bf】,那么位于正中间的就是bc。
思路:等同于给你两个26进制数,求这两个数中间平均数。先求出差值,差值再除2,然后再和s相加。就是模拟26进制下的加减除法(也可以两个相加再除2就不用模拟减法了orz当时没想这样)。
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int n;
char s[maxn],t[maxn];
int ans[maxn];
int main()
{
int i;
int x,y;
while(cin>>n)
{
getchar();
memset(s,,sizeof(s));
memset(t,,sizeof(t));
gets(s);
gets(t);
for(i=n-;i>=;i--)//低位到高位模拟减法和除法
{
x = t[i] - 'a';
y = s[i] - 'a';
if(x - y < )//当前位不够减就借位
{
t[i-]--;
x += ;//注意借来的是十进制下的26
}
if((x-y)% == )//模拟除法
ans[i] = (x-y)/;
else//该位是奇数
{
ans[i] = (x-y)/;
ans[i+] += ;//给后一位13(即这一位除2最后有0.5,就等于26进制下的13)
//这个过程可能会导致后一位超出26,下面再模拟一下加法取余即可
}
}
for(i=n-;i>=;i--)//低位到高位模拟加法
{
x = s[i] - 'a';
ans[i-] += (x+ans[i])/;//高位进位
ans[i] = (x+ans[i])%;//低位取余
}
for(i=;i<n;i++)
cout<<char(ans[i]+'a');
cout<<endl;
}
return ;
}
Codeforces Round #550 (Div. 3) E. Median String (模拟)的更多相关文章
- Codeforces Round #550 (Div. 3)E. Median String
把字符串看作是26进制的数,从后往前翻译,那么就可以把两个串变成对应的26进制的数字,那么只要把两个数加起来除以二就得到中间的串对应的数了,同理再转化回来就行了.但是这样会有一个问题就是串的长度有2e ...
- Codeforces Round #550 (Div. 3) E. Median String (思维,模拟)
题意:给你两个字符串\(s\)和\(t\),保证\(t\)的字典序大于\(s\),求他们字典序中间的字符串. 题解:我们假设题目给的不是字符串,而是两个10禁止的正整数,那么输出他们之间的数只要把他两 ...
- 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String
题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...
- Codeforces Round #368 (Div. 2) B. Bakery (模拟)
Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...
- CodeForces Round #550 Div.3
http://codeforces.com/contest/1144 A. Diverse Strings A string is called diverse if it contains cons ...
- Codeforces Round #284 (Div. 2)A B C 模拟 数学
A. Watching a movie time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #285 (Div. 2) A B C 模拟 stl 拓扑排序
A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- Codeforces Round #327 (Div. 2) C. Median Smoothing 找规律
C. Median Smoothing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/p ...
- Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)
题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...
随机推荐
- Coursera-AndrewNg(吴恩达)机器学习笔记——第四周编程作业(多分类与神经网络)
多分类问题——识别手写体数字0-9 一.逻辑回归解决多分类问题 1.图片像素为20*20,X的属性数目为400,输出层神经元个数为10,分别代表1-10(把0映射为10). 通过以下代码先形式化展示数 ...
- 第 15 章 位操作(binbit)
/*------------------------------------ binbit.c -- 使用位操作显示二进制 ------------------------------------*/ ...
- mysql用户管理与权限
1.设置密码 set password for 用户名@localhost = password('密码'); 2.取消密码 set password for 用户名@localhost = pass ...
- 基于CNN网络的汉字图像字体识别及其原理
现代办公要将纸质文档转换为电子文档的需求越来越多,目前针对这种应用场景的系统为OCR系统,也就是光学字符识别系统,例如对于古老出版物的数字化.但是目前OCR系统主要针对文字的识别上,对于出版物的版面以 ...
- SDN期末作业验收
作业链接:https://edu.cnblogs.com/campus/fzu/SoftwareDefinedNetworking2017/homework/1585 负载均衡程序 1.github链 ...
- SDN 第四次上机作业
1.建立以下拓扑,并连接上ODL控制器. 2.利用ODL下发流表,使得h3在10s内ping不通h1,10s后恢复. 3.借助Postman通过ODL的北向接口下发流表,再利用ODL北向接口查看已下发 ...
- linux虚拟化课堂总结图2019_4_23
- CentOS 6.5安装配置NFS服务器
OS:centos 6.5 服务端:10.1.11.201 客户端:10.1.11.202 10.1.11.203 10.1.11.204 1.服务端的配置 安装必须的yum包: yum -y ins ...
- (转载)c++引用
引用,顾名思义是某一个变量或对象的别名,对引用的操作与对其所绑定的变量或对象的操作完全等价 语法:类型 &引用名=目标变量名: 特别注意: 1.&不是求地址运算符,而是起标志作用 2. ...
- filebeat配置
filebeat收集日志配置: filebeat.prospectors: - input_type: log enabled: true paths: - /mydata/erp_datacente ...