CodeForces - 1144E 【模拟,进制】

题意:给出长度为n的两个串,求他们中位数串。比如aa cc,中位数串就是bb,题目保证这俩中间的串个数是奇数。

思路:就是26进制的加法模拟,再/2。QAQ习惯性大数写成结构体,局部爆栈了,愣是看不出来,加static才行,不过这里直接用数组就行了。

代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxv = 2e5 + 5;

int n;
string s1, s2;
int a[maxv], b[maxv];

int main()
{
	cin >> n;
	cin >> s1;
	cin >> s2;
	int cnt = 0,up=0;
	for (int i = n - 1; i >= 0; i--)
	{
		int now = s1[i] - 'a' + s2[i] - 'a' + up;
		a[cnt++] = now % 26;
		up = now / 26;
	}
	if (up)
	{
		a[cnt++] = up;
	}
	int lst = 0;
	int ct = 0;
	for (int i = cnt - 1; i >= 0; i--)
	{
		int now = lst * 26 + a[i];
		if (now < 2)
		{
			b[i] = 0;
			lst = now;
		}
		else
		{
			b[i] = now / 2;
			lst = now % 2;
		}
	}
	for (int i = cnt-1; i >=0; i--)
	{
		if (i == cnt - 1 && cnt > n&&b[i] == 0)
			continue;
		printf("%c", b[i] + 'a');
	}
	cout << endl;
	return 0;
}

 

你可能感兴趣的:(模拟)