CodeForces

题目链接

题目大意

  给两个长度相等的字符串s和t,求在字典序在他们之间的字符串之中中间的那个字符串。

解题思路

  本题利用了字典序与数位制的一致性,所求的其实就是两个字符串转成26进制之后的中位数,和大数加法一样的思路。

代码

const int maxn = 2e5+10;
const int maxm = 2e5+10;
char s[maxn], t[maxn], c[maxn];
int n;
int main() {
	cin >> n >> s+1 >> t+1;
	int carry = 0;
	for (int i = n; i>=1; --i) {
		s[i] -= 'a';
		t[i] -= 'a';
		int res = s[i]+t[i]+carry;
		carry = res/26;
		if (i!=1) res %= 26; 
		//进位,i=1也可以进位,不过最后还得退回来,所以没必要
		c[i] = res;
	}
	carry = 0;
	for (int i = 1; i<=n; ++i) {
		int res = c[i]+carry*26;
		c[i] = res/2+'a';
		carry = res%2;
		//借位给低位
	}
	cout << c+1 << endl;
	return 0;
} 
原文地址:https://www.cnblogs.com/shuitiangong/p/14421616.html