Median String
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

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.

Examples
input
2
az
bf
output
bc
input
5
afogk
asdji
output
alvuw
input
6
nijfvj
tvqhwp
output
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 (模拟)的更多相关文章

  1. Codeforces Round #550 (Div. 3)E. Median String

    把字符串看作是26进制的数,从后往前翻译,那么就可以把两个串变成对应的26进制的数字,那么只要把两个数加起来除以二就得到中间的串对应的数了,同理再转化回来就行了.但是这样会有一个问题就是串的长度有2e ...

  2. Codeforces Round #550 (Div. 3) E. Median String (思维,模拟)

    题意:给你两个字符串\(s\)和\(t\),保证\(t\)的字典序大于\(s\),求他们字典序中间的字符串. 题解:我们假设题目给的不是字符串,而是两个10禁止的正整数,那么输出他们之间的数只要把他两 ...

  3. 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String

    题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...

  4. Codeforces Round #368 (Div. 2) B. Bakery (模拟)

    Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...

  5. CodeForces Round #550 Div.3

    http://codeforces.com/contest/1144 A. Diverse Strings A string is called diverse if it contains cons ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)

    题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...

随机推荐

  1. glViewport()函数和glOrtho()函数的理解(转)

    http://www.cnblogs.com/yxnchinahlj/archive/2010/10/30/1865298.html 在OpenGL中有两个比较重要的投影变换函数,glViewport ...

  2. 第 14 章 结构和其他数据形式(伸缩型数组成员C99)

    伸缩型数组成员C99 声明一个伸缩型数组成员的规则: 1.伸缩型数组成员必须是结构的最后一个成员: 2.结构中必须至少有一个成员: 3.伸缩数组的方括号是空的. 示例 struct flex { in ...

  3. String真的是不可变的吗?

    你可能问一个人String是可变的吗?想必他们都会一口同生的说String是不可变的,因为String是final修饰的,而且它底层的是final修饰的char[]数组. 可以看到String源码: ...

  4. 前端aes解密实战小结

    很多人对于AES加密并不是很了解,导致互相之间进行加密解密困难. 本文用简单的方式来介绍AES在使用上需要的知识,而不涉及内部算法.最后给出例子来帮助理解AES加密解密的使用方法. AES的麻烦 相比 ...

  5. 声明式编程:程序=数据+逻辑(what)+算法(控制+计算)

    接口:what: 实现:算法:指令: 编程语言中,凡是不涉及到算法的部分,都可以认为是声明式编程. 命令式编程可以与算法划等号:算法要求严格的计算逻辑和控制,是实施细节的精准描述: 命令式编程与声明式 ...

  6. ES6标准入门之正则表达式的拓展

    所谓正则表达式,又称规则表达式.(英语:Regular Expression,在代码中常简写为regex.regexp或RE),计算机科学的一个概念.正则表达式通常被用来检索.替换那些符合某个模式(规 ...

  7. java任务调度框架

    https://www.ibm.com/developerworks/cn/java/j-lo-taskschedule/

  8. thinkphp5整合 gatewaywork实现聊天

    1:将下载的gatewaywork下的\vendor  下的workman文件夹,整个复制到tp5下的vendor目录下 2:tp5\application\push   新键push文件夹,将下载的 ...

  9. 连接远程数据库ORACLE11g,错误百出!

    客户机中PLSQL DEV访问虚拟机中的ORACLE11g,错误百出! 创建时间: 2017/10/14 18:44 作者: CNSIMO 标签: ORACLE 忙了一下午,只有两个字形容:麻烦!   ...

  10. 实例详解:MFC坐标轴实现

    需求:MFC坐标轴实现-----最好有步骤啊,刚刚才接触C++和MFC啊.MFC怎样在特定区域建立坐标轴,x轴自适应,y轴有固定范围,最好有网格. 解决思路:VC 内存绘图,不闪屏,具体代码如下: / ...