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

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 (1k21051≤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=200005;
int n;
char s[maxn],t[maxn];
int ans[maxn];
int main()
{
    int i;
    int x,y;
    while(cin>>n)
    {
        getchar();
        memset(s,0,sizeof(s));
        memset(t,0,sizeof(t));
        gets(s);
        gets(t);
        for(i=n-1;i>=0;i--)//低位到高位模拟减法和除法 
        {
            x = t[i] - 'a';
            y = s[i] - 'a';
            if(x - y < 0)//当前位不够减就借位 
            {
                t[i-1]--;
                x += 26;//注意借来的是十进制下的26 
            }
            if((x-y)%2 == 0)//模拟除法 
            ans[i] = (x-y)/2;
            else//该位是奇数
            {
                ans[i] = (x-y)/2;
                ans[i+1] += 13;//给后一位13(即这一位除2最后有0.5,就等于26进制下的13)
                                //这个过程可能会导致后一位超出26,下面再模拟一下加法取余即可  
            }
        }
        for(i=n-1;i>=0;i--)//低位到高位模拟加法 
        {
            x = s[i] - 'a';
            ans[i-1] += (x+ans[i])/26;//高位进位 
            ans[i] = (x+ans[i])%26;//低位取余 
        }
        for(i=0;i<n;i++)
        cout<<char(ans[i]+'a');
        cout<<endl;
    }
    return 0;
}

 

posted @ 2019-04-01 00:38  CV小萌新  阅读(651)  评论(0编辑  收藏  举报