Codeforces Round #550 (Div. 3) E. Median String (模拟)
2 seconds
256 megabytes
standard input
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.
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.
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.
2
az
bf
bc
5
afogk
asdji
alvuw
6
nijfvj
tvqhwp
qoztvz
题意:给定两个长度为n的字符串s和t,它们按照字典序排列有t>s,求字符串按照字典序排列位于s和t中间位置的那个字符串(题目保证在正中间)。比如样例az和bf,它们两个按照字典序排列的字符串集合是【az,ba,bb,bc,bd,be,bf】,那么位于正中间的就是bc。
思路:等同于给你两个26进制数,求这两个数中间平均数。先求出差值,差值再除2,然后再和s相加。就是模拟26进制下的加减除法(也可以两个相加再除2就不用模拟减法了orz当时没想这样)。
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int n;
char s[maxn],t[maxn];
int ans[maxn];
int main()
{
int i;
int x,y;
while(cin>>n)
{
getchar();
memset(s,,sizeof(s));
memset(t,,sizeof(t));
gets(s);
gets(t);
for(i=n-;i>=;i--)//低位到高位模拟减法和除法
{
x = t[i] - 'a';
y = s[i] - 'a';
if(x - y < )//当前位不够减就借位
{
t[i-]--;
x += ;//注意借来的是十进制下的26
}
if((x-y)% == )//模拟除法
ans[i] = (x-y)/;
else//该位是奇数
{
ans[i] = (x-y)/;
ans[i+] += ;//给后一位13(即这一位除2最后有0.5,就等于26进制下的13)
//这个过程可能会导致后一位超出26,下面再模拟一下加法取余即可
}
}
for(i=n-;i>=;i--)//低位到高位模拟加法
{
x = s[i] - 'a';
ans[i-] += (x+ans[i])/;//高位进位
ans[i] = (x+ans[i])%;//低位取余
}
for(i=;i<n;i++)
cout<<char(ans[i]+'a');
cout<<endl;
}
return ;
}
Codeforces Round #550 (Div. 3) E. Median String (模拟)的更多相关文章
- Codeforces Round #550 (Div. 3)E. Median String
把字符串看作是26进制的数,从后往前翻译,那么就可以把两个串变成对应的26进制的数字,那么只要把两个数加起来除以二就得到中间的串对应的数了,同理再转化回来就行了.但是这样会有一个问题就是串的长度有2e ...
- Codeforces Round #550 (Div. 3) E. Median String (思维,模拟)
题意:给你两个字符串\(s\)和\(t\),保证\(t\)的字典序大于\(s\),求他们字典序中间的字符串. 题解:我们假设题目给的不是字符串,而是两个10禁止的正整数,那么输出他们之间的数只要把他两 ...
- 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String
题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...
- Codeforces Round #368 (Div. 2) B. Bakery (模拟)
Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...
- CodeForces Round #550 Div.3
http://codeforces.com/contest/1144 A. Diverse Strings A string is called diverse if it contains cons ...
- Codeforces Round #284 (Div. 2)A B C 模拟 数学
A. Watching a movie time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #285 (Div. 2) A B C 模拟 stl 拓扑排序
A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- Codeforces Round #327 (Div. 2) C. Median Smoothing 找规律
C. Median Smoothing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/p ...
- Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)
题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...
随机推荐
- Python学习--- requests库中文编码问题
为什么会有ISO-8859-1这样的字符集编码 requests会从服务器返回的响应头的 Content-Type 去获取字符集编码,如果content-type有charset字段那么request ...
- 1.4环境的准备(四)之Pycharm的使用技巧
返回总目录 目录: 1.快捷键的使用: 2.提示技巧: 3.其他技巧: (一)快捷键的使用: (1)Pycharm自带默认的快捷键 1.Ctrl + C 复制 2.Ctrl + V 粘贴 3.Ctrl ...
- 团队作业——Beta冲刺3
团队作业--Beta冲刺 冲刺任务安排 杨光海天 今日任务:浏览详情界面的开发 明日任务:浏览详情界面的开发 吴松青 今日任务:与队长一同进行图片详情的开发,接触了一些自己没接触过的知识点并向队友学习 ...
- 团队作业——Alpha冲刺 9/12
团队作业--Alpha冲刺 冲刺任务安排 杨光海天 今日任务:修复编辑界面与弹窗界面合并中出现的BUG 明日任务:希望完成编辑界面所有接口交互的功能 郭剑南 今日任务:优化图像预处理所有功能的函数代码 ...
- 【Android自动化】在使用uiautomator框架自动化时,往往有时再运行脚本时发现xxx实例属性不被允许
例如: # -*- coding:utf-8 -*- from uiautomator import device as d d(classname="android.widget.List ...
- Python接口自动化--requests 2
# _*_ encoding:utf-8 _*_ import json import requests #post请求 payload = {"cindy":"hell ...
- css实现常用的两栏三栏布局
1.两栏 <div class="wrapper"> <div class="half left">left box <p> ...
- python第二十八课——编码小常识
2.内存和硬盘: 内存:计算机硬件组成部分之一,它是一个容器,用来存储数据:处理数据速度快, 存储数据量小:断电死机数据会丢失,短暂性存储数据 硬盘:计算机硬件组成部分之一,它是一个容器,用来存储数据 ...
- node.js cheerio API
安装 npm install cheerio load var cheerio = require('cheerio'), $ = cheerio.load('<ul id=“fruits”&g ...
- Django使用AJAX调用自己写的API接口
Django使用AJAX调用自己写的API接口 *** 具体代码和数据已上传到github https://github.com/PythonerKK/eleme-api-by-django-rest ...