zoukankan      html  css  js  c++  java
  • Median String

    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 (1k21051≤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 kk lexicographically not less than ss and not greater than tt.

    Examples

    Input
    2
    az
    bf
    
    Output
    bc
    
    Input
    5
    afogk
    asdji
    
    Output
    alvuw
    
    Input
    6
    nijfvj
    tvqhwp
    
    Output
    qoztvz

    题目大致题意就是给你两个字符串数组,让你用计算他们的中间值,即转化为26进制后的中间值,
    我的想法就是把每一位进行两个数组的加法,通过每一位的进位来计算,对每一个数值mod26,如果大于26,就向前进一位
    最后计算的时候如果是偶数就是直接取中间值,如果奇数就想下一位;
    接下来就是代码
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    #define ll long long 
    const int maxn = 200100;
    const int inf = 0x3f3f3f3f;
    char a[maxn], b[maxn];
    int c[maxn], d[maxn],e[maxn];
    int main()
    {
        int k;
        cin >> k;
        cin >> a;
        cin >> b;
        for (int i = k-1;i >=0 ;i--)
        {
            c[i] = a[i] - 'a';
            d[i] = b[i] - 'a' ;
        }
        memset(e, 0, sizeof(e));
        for (int i = k - 1;i >= 0;i--)
        {
            e[i] += c[i] + d[i];
            if (e[i] >= 26&&i!=0)
            {
                e[i] = e[i] % 26;
                e[i - 1] += 1;
                
            }
        }
        for (int  i = 0; i < k; i++)
        {
            if (e[i] % 2 == 0)
            {
                printf("%c", e[i] / 2 + 'a');
            }
            else
            {
                printf("%c", e[i] / 2 + 'a');
                e[i + 1] += 26;
            }
        }
    }
  • 相关阅读:
    函数式 js 接口实现原理,以及 lodash/fp 模块
    谈谈混合 App Web 资源的打包与增量更新
    如何实现 javascript “同步”调用 app 代码
    如何发布带静态资源的库——android 篇
    [老文章搬家] 关于屏蔽优酷视频广告的一个方法
    [老文章搬家] 关于 Huffman 编码
    [老文章搬家] 插件化软件设计的头疼问题以及可能的解决思路
    [老文章搬家] [翻译] 深入解析win32 crt 调试堆
    Qt 5.0+ 中 connect 新语法与重载函数不兼容问题的解决方法,以及个人看法
    武佩奇 DJango博客地址
  • 原文地址:https://www.cnblogs.com/csxaxx/p/10831981.html
Copyright © 2011-2022 走看看