CodeForces1006D-Two Strings Swaps
D. Two Strings Swaps
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given two strings aa and bb consisting of lowercase English letters, both of length nn. The characters of both strings have indices from 11 to nn, inclusive.
You are allowed to do the following changes:
- Choose any index ii (1≤i≤n1≤i≤n) and swap characters aiai and bibi;
- Choose any index ii (1≤i≤n1≤i≤n) and swap characters aiai and an−i+1an−i+1;
- Choose any index ii (1≤i≤n1≤i≤n) and swap characters bibi and bn−i+1bn−i+1.
Note that if nn is odd, you are formally allowed to swap a⌈n2⌉a⌈n2⌉ with a⌈n2⌉a⌈n2⌉ (and the same with the string bb) but this move is useless. Also you can swap two equal characters but this operation is useless as well.
You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps.
In one preprocess move you can replace a character in aa with another character. In other words, in a single preprocess move you can choose any index ii (1≤i≤n1≤i≤n), any character cc and set ai:=cai:=c.
Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings aa and bb equal by applying some number of changes described in the list above.
Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess movesto the string bb or make any preprocess moves after the first change is made.
Input
The first line of the input contains one integer nn (1≤n≤1051≤n≤105) — the length of strings aa and bb.
The second line contains the string aa consisting of exactly nn lowercase English letters.
The third line contains the string bb consisting of exactly nn lowercase English letters.
Output
Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string aa equal to string bb with a sequence of changes from the list above.
Examples
input
Copy
7
abacaba
bacabaa
output
Copy
4
input
Copy
5
zcabd
dbacz
output
Copy
0
Note
In the first example preprocess moves are as follows: a1:=a1:='b', a3:=a3:='c', a4:=a4:='a' and a5:=a5:='b'. Afterwards, a=a="bbcabba". Then we can obtain equal strings by the following sequence of changes: swap(a2,b2)swap(a2,b2) and swap(a2,a6)swap(a2,a6). There is no way to use fewer than 44preprocess moves before a sequence of changes to make string equal, so the answer in this example is 44.
In the second example no preprocess moves are required. We can use the following sequence of changes to make aa and bb equal: swap(b1,b5)swap(b1,b5), swap(a2,a4)swap(a2,a4).
Codeforces (c) Copyright 2010-2018 Mike Mirzayanov
The only programming contests Web 2.0 platform
Server time: Jul/18/2018 00:49:41UTC+8 (d2).
Desktop version, switch to mobile version.
题解:
这个题其实就是枚举比较多。。。多考虑考虑;我们可以分别考虑每一个环(即a[i] a[n-i+1] b[i] b[n-i+1])所含字母种类,然后分别对每一种讨论即可;
AC代码为:
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
long long cnt=0;
string s1,s2;
cin>>n;
cin>>s1>>s2;
for(int i=0;i<n/2;i++)
{
set<int> s;
s.insert(s1[i]-'a'+1);
s.insert(s1[n-i-1]-'a'+1);
s.insert(s2[i]-'a'+1);
s.insert(s2[n-i-1]-'a'+1);
if(s.size()==3)
{
if(s1[i]==s1[n-i-1]) cnt+=2;
else cnt++;
}
else if(s.size()==4) cnt+=2;
else if(s.size()==2)
{
if(s1[i]==s1[n-i-1]&&s1[i]==s2[i] || s1[i]==s1[n-i-1]&&s1[i]==s2[n-i-1] || s1[i]==s2[i]&&s2[i]==s2[n-i-1] || s1[n-i-1]==s2[i]&&s2[i]==s2[n-i-1])
cnt++;
}
}
if((n&1) && s1[n/2]!=s2[n/2]) cnt++;
cout<<cnt<<endl;
return 0;
}
CodeForces1006D-Two Strings Swaps的更多相关文章
- 【Codeforces 1006D】Two Strings Swaps
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 注意只能改变a不能改变b 然后只要让a[i],b[i],a[n-i-1],b[n-i-1]这4个字符能凑成两对.全都一样就可以了 分类讨论下就 ...
- Codeforces Round#498(Div.3)D. Two Strings Swaps
题目 题意是给了两个字符串a和b,然后可以对这两个字符串有三种操作来使这两个字符串相等,一是交换a[i]和b[i],二是交换a[i]和a[n-i+1],三是交换b[i]和b[n-i+1],这三个操作都 ...
- Codeforces Round #498 (Div. 3) D. Two Strings Swaps (思维)
题意:给你两个长度相同的字符串\(a\)和\(b\),你可以将相同位置上的\(a\)和\(b\)的字符交换,也可以将\(a\)或\(b\)中某个位置和对应的回文位置上的字符交换,这些操作是不统计的,你 ...
- Codeforces Div3 #498 A-F
. A. Adjacent Replacement ...
- Codeforces Round #498 (Div. 3) 简要题解
[比赛链接] https://codeforces.com/contest/1006 [题解] Problem A. Adjacent Replacements [算法] 将序列中的所有 ...
- 【leetcode】1247. Minimum Swaps to Make Strings Equal
题目如下: You are given two strings s1 and s2 of equal length consisting of letters "x" and &q ...
- Codeforces Round #619 (Div. 2) A. Three Strings
You are given three strings aa , bb and cc of the same length nn . The strings consist of lowercase ...
- Hacker Rank: Two Strings - thinking in C# 15+ ways
March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...
- StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?
StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...
随机推荐
- Pandas进阶笔记 (一) Groupby 重难点总结
如果Pandas只是能把一些数据变成 dataframe 这样优美的格式,那么Pandas绝不会成为叱咤风云的数据分析中心组件.因为在数据分析过程中,描述数据是通过一些列的统计指标实现的,分析结果也需 ...
- 201871010114-李岩松《面向对象程序设计(java)》第八周学习总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...
- Jenkins 与Docker/Kubernetes的自动化CI流水(笔记)
一.CI/CD 持续集成(continuous Integration,CI):代码合并.构建.部署.测试都在一起.不断执行这个过程,并对结果反馈. 持续部署(Continuous Deploymen ...
- C++中对封装的语法支持——重载运算符
重载运算符 1.对于自定义类型,编译器不知道运算规则,而重载运算符会将两个对象相加转换为函数调用. 2.运算符重载转换的函数调用,函数名字是固定的规则. (1) 如果重载+号运算符,函数名字就是:op ...
- Laravel生命周期与原理
一旦用户(浏览器)发送了一个HTTP请求,我们的apache或者nginx一般都转到index.php,因此,之后的一系列步骤都是从index.php开始的,我们先来看一看这个文件代码. <?p ...
- Netty学习篇⑤--编、解码
前言 学习Netty也有一段时间了,Netty作为一个高性能的异步框架,很多RPC框架也运用到了Netty中的知识,在rpc框架中丰富的数据协议及编解码可以让使用者更加青睐: Netty支持丰富的编解 ...
- 【SQL SERVER】2017 Developer 安装教程
官网下载地址:https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 1.下载之后双击exe文件,选择基本 自定义都行 2.选择 ...
- nyoj 324-猴子吃桃问题 (m[i] = (m[i-1] + 1) * 2)
324-猴子吃桃问题 内存限制:64MB 时间限制:3000ms 特判: No 通过数:20 提交数:21 难度:0 题目描述: 有一堆桃子不知数目,猴子第一天吃掉一半,又多吃了一个,第二天照此方法, ...
- 力扣(LeetCode)Excel表列序号 个人题解
给定一个Excel表格中的列名称,返回其相应的列序号. 例如, A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ...
- 在VMware通过挂载系统光盘搭建本地yum仓库
1.首先需要有一个VMware虚拟机: 2.进去虚拟机(这里用Linux下deCentOS进行演示): 3.用root账号进行登录,否则在根目录下没有一些操作权限: 4.打开终端: 5,输入命令“cd ...