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");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值