E. Median String

You are given two strings s and t, both consisting of exactly k lowercase Latin letters, s is lexicographically less than t.

Let’s consider list of all strings consisting of exactly k lowercase Latin letters, lexicographically not less than s and not greater than t (including s and t) in lexicographical order. For example, for k=2, s=“az” and 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 s and not greater than t.

Input
The first line of the input contains one integer k (1≤k≤2⋅105) — the length of strings.

The second line of the input contains one string s consisting of exactly k lowercase Latin letters.

The third line of the input contains one string t consisting of exactly k lowercase Latin letters.

It is guaranteed that s is lexicographically less than t.

It is guaranteed that there is an odd number of strings lexicographically not less than s and not greater than t.

Output

Print one string consisting exactly of k lowercase Latin letters — the median (the middle element) of list of strings of length k lexicographically not less than s and not greater than t.

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当时没想这样)。

(题意与思路转自一位大佬的,想进去看看大佬写的,点下面的链接就行:https://www.cnblogs.com/chdforestsea/p/10634200.html)

代码:

(看过大佬的后自己打的)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
#define ll long long
#define ms(x,y) memset(x,y,sizeof(x))
const int maxn=200005;
int n,x,y;
char s[maxn],t[maxn];
int ans[maxn];
int main(){
	while(cin>>n){
		ms(s,0);ms(t,0);
		cin>>s>>t;
		for(int i=n-1;i>=0;i--){
			x=t[i]-'a';
			y=s[i]-'a';
			if(x<y){
				t[i-1]--;
				x+=26;
			}
			ans[i]=(x-y)/2;
			if((x-y)%2!=0){
				ans[i+1]+=13;
			}
		}
		for(int i=n-1;i>=0;i--){
			y=s[i]-'a';
			ans[i-1] += (y+ans[i])/26;
            ans[i] = (y+ans[i])%26;
		}
		for(int i=0;i<n;i++)
			cout<<(char)(ans[i]+'a');
			cout<<endl;
	} 
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44417851/article/details/88978373