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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值