原题链接:http://codeforces.com/contest/1144/problem/E
题目原文:
E. Median String
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given two strings ss and tt, both consisting of exactly kk lowercase Latin letters, ss is lexicographically less than tt.
Let's consider list of all strings consisting of exactly kk lowercase Latin letters, lexicographically not less than ss and not greater than tt(including ss and tt) in lexicographical order. For example, for k=2k=2, s=s="az" and t=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 ss and not greater than tt.
Input
The first line of the input contains one integer kk (1≤k≤2⋅1051≤k≤2⋅105) — the length of strings.
The second line of the input contains one string ss consisting of exactly kk lowercase Latin letters.
The third line of the input contains one string tt consisting of exactly kk lowercase Latin letters.
It is guaranteed that ss is lexicographically less than tt.
It is guaranteed that there is an odd number of strings lexicographically not less than ss and not greater than tt.
Output
Print one string consisting exactly of kk lowercase Latin letters — the median (the middle element) of list of strings of length kklexicographically not less than ss and not greater than tt.
Examples
input
Copy
2 az bfoutput
Copy
bcinput
Copy
5 afogk asdjioutput
Copy
alvuwinput
Copy
6 nijfvj tvqhwpoutput
Copy
qoztvz
题目大意:
给两个字符串s和t,s的字典序小于t,求字典序大于等于s小于等于t 的字符串的中间一个。保证字符串集合为奇数个。
解题思路 :
把其当成26进制的大数运算,ans = (s + t) / 2.
AC代码:
#include <cstdio>
#include <cstring>
using namespace std;
const int N = int(2e5 + 5);
const int RADIX = 26; // 基数
int g_s[N];
int g_t[N];
int g_len;
void init()
{
memset(g_s, 0, sizeof(g_s));
memset(g_t, 0, sizeof(g_t));
}
void add()
{
int i, t, c = 0;
for (i = g_len; i >= 1; i--)
{
t = g_s[i] + g_t[i] + c;
g_s[i] = t % RADIX;
c = t / RADIX;
}
g_s[0] = c;
}
void divide2()
{
int d = 2;
int i, t, r = 0;
for (i = 0; i <= g_len; i++)
{
t = g_s[i] + r * RADIX;
g_s[i] = t / d;
r = t % d;
}
// 由题意知总能除尽
}
int main()
{
int i;
char c;
while (scanf("%d", &g_len) != EOF)
{
init();
for (i = 1; i <= g_len; i++)
{
scanf(" %1c", &c);
g_s[i] = c - 'a';
}
for (i = 1; i <= g_len; i++)
{
scanf(" %1c", &c);
g_t[i] = c - 'a';
}
add();
divide2();
for (i = 1; i <= g_len; i++)
{
printf("%1c", g_s[i] + 'a');
}
printf("\n");
}
return 0;
}