CodeForces - 1144E(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.
样例

输入:
2
az
bf
输出:
bc

输入:
5
afogk
asdji
输出:
alvuw

输入:
6
nijfvj
tvqhwp
输出:
qoztvz

题意:
给定两个字符串,找出这两个字符串的中间字符串。

思路:
一道字符串上大模拟在加上一点思维吧,感觉写完这题对字符串的理解也更加透彻了一些。很简单的理解为26进制的加法和除法就可以了,留作纪念(^_−)☆

AC代码:

#include<stdio.h>
const int maxn = 2e5 + 10;
char s[maxn], t[maxn], ans[maxn];
int main()
{
	int n;
	scanf("%d%s%s", &n, s, t);
	for (int i = 0; i < n; i++){
		s[i] = s[i] - 'a';
		t[i] = t[i] - 'a';
	}
	for (int i = n - 1; i >= 0; i--){
		int d = s[i] + t[i];
		if (d % 2 == 0) 
			ans[i] = d / 2;
		else{
			ans[i + 1] += 13;
			ans[i] = (d + ans[i + 1] / 26) / 2;
			ans[i + 1] %= 26;
			ans[i] %= 26;
		}
	}
	for (int i = 0; i < n; i++) 
		printf("%c", ans[i] + 'a');
	printf("\n");
}
发布了40 篇原创文章 · 获赞 6 · 访问量 1405

猜你喜欢

转载自blog.csdn.net/qq_43321732/article/details/104181514