Median String

                     在看代码的小可爱们评论点赞好不好鸭!!

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
2
input
az
bf

output
bc

5
input
afogk
asdji

output
alvuw

6
input
nijfvj
tvqhwp

output
qoztvz

解题思路:字符串和数组之间的转换,然后大数减法大数加法

需要注意数组内除法时可能出现的奇偶数问题

代码内只有输入用到了c++中cin的输入,不会c++的小可爱不要直接Alt+F4

#include<stdio.h>//代码内只有输入用到了c++中cin的输入,不会c++的小可爱不要直接Alt+F4
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<iostream>//标准输入输出函数库
using namespace std;

int a[200005];//数组a用来储存先输入的字典序小的字符串
int b[200005];//数组b用来储存先输入的字典序大的字符串
int c[200005];//数组c用来储存 数组b 减 数组a 之后的差数组
char str1[200005];
char str2[200005];

int main(){
	int n;
	while(scanf("%d",&n)!=EOF){
		cin>>str1>>str2;//输入字符串1和2
		int i;
		for(i=0;i<n;i++){
			a[i]=str1[i]-'a';//把字符串1存到数组1中
			b[i]=str2[i]-'a';//把字符串2存到数组2中
		}
		for(i=n-1;i>=0;i--){            //大数减法
			if(b[i]>=a[i]){
				c[i]=b[i]-a[i];
			}
			else{
				c[i]=b[i]-a[i]+26;
				b[i-1]--;
			}
		}
		for(i=0;i<n;i++){          //取半
			if(c[i]%2==0){
				c[i]=c[i]/2;
			}
			else{
				c[i]=c[i]/2;
				c[i+1]+=26;
			}
		}
		for(i=n-1;i>=0;i--){      //小数组加上取半后的数组
			a[i]=a[i]+c[i];
			while(a[i]>=26){
				a[i]-=26;
				a[i-1]++;
			}
		}
		for(i=0;i<n;i++){          //加上一个强制类型转换就是我们的结果啦!
			printf("%c",(char)(a[i]+'a'));
		}
		printf("\n");
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值