CodeForces - 1144E题解

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.

example

Input
2
az
bf
Output
bc
Input
5
afogk
asdji
Output
alvuw
Input
6
nijfvj
tvqhwp
Output
qoztvz

今天练习赛的一道题,大意为给定两个字符串,按字典序取中间的字符串,如az和bf,依次为az,ba,bb,bc,bd,be,bf,所以输出bc。
这个题我开始的想法是得出给定两字符串之间相差然后除二再对第一个字符串直接进行操作,结果样例事都过了,但是不知道为什么第六组数据被卡了,也没找到到底是哪里出了问题,后来再倒过头去仔细想了想,其实就是转了个26进制出来,但是转进制这方面的知识点被卡了,找了个模板套上了
后来结束了回来又扒拉了一下,整理了一下,也去网上看了看大佬的代码
AC码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
 
char s1[200005];
char s2[200005];
int a[200005];
 
inline int getv(char ch)
{
	return ch-'a';
}
 
int main()
{
	int n;
	scanf("%d",&n);
	scanf("%s%s",s1+1,s2+1);
	for(int i=1;i<=n;i++)
		a[i]=getv(s1[i])+getv(s2[i]);
	for(int i=n;i>=2;i--)
	{
		a[i-1]+=a[i]/26;
		a[i]%=26;
	}
	for(int i=1;i<=n;i++)
	{
		if(a[i]&1)
			a[i+1]+=26;
		putchar('a'+a[i]/2);
	}
	putchar('\n');
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43141958/article/details/89076151