Two Strings Swaps(CF-1006D)
Problem Description
You are given two strings a and b consisting of lowercase English letters, both of length n. The characters of both strings have indices from 1 to n, inclusive.
You are allowed to do the following changes:
Choose any index i (1≤i≤n) and swap characters ai?and bi;
Choose any index i (1≤i≤n) and swap characters ai?and an?i+1;
Choose any index i (1≤i≤n) and swap characters bi?and bn?i+1.
Note that if nn is odd, you are formally allowed to swap a?n2??with a?n2? (and the same with the string b) 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 i?(1≤i≤n), any character cc and set ai:=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 moves to 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≤10^5) — the length of strings a and b.
The second line contains the string a consisting of exactly n lowercase English letters.
The third line contains the string b consisting of exactly n 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 a equal to string b with a sequence of changes from the list above.
Examples
Input
7
abacaba
bacabaa
Output
4?
Input
5
zcabd
dbacz
Output
0
題意:給兩個(gè)長度為 n 的字符串a(chǎn)與串b,可以交換 ai 與 an-i+1,交換 ai 與 bi,交換 bi 與 bn-i+1,現(xiàn)要使得通過以上3種操作使得字符串相同,但大多數(shù)情況不可能,因此要進(jìn)行預(yù)處理,改變串a(chǎn),使得改變后可以滿足條件,從而相同。
思路:
改變字符串的操作,實(shí)質(zhì)是對(duì)4個(gè)位置進(jìn)行交換,即 ai、an?i+1、bi、bn?i+1 這幾個(gè)字符的位置隨意交換。?
因此只要保證這4個(gè)位置只有2種字符,每種字符有2個(gè)或者只有1種字符即可。
先統(tǒng)計(jì)4個(gè)位置的字符,再進(jìn)行分類討論:
思路來源:點(diǎn)擊這里
Source Program
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<string> #include<cstdlib> #include<queue> #include<set> #include<map> #include<stack> #include<ctime> #include<vector> #define INF 0x3f3f3f3f #define PI acos(-1.0) #define N 1000005 #define MOD 123 #define E 1e-6 using namespace std; int n; int num[27]; char str1[N],str2[N]; int main() {scanf("%d",&n);scanf("%s%s",str1+1,str2+1);int ans=0;if(n&1)//中間的兩個(gè)字符ans+=(str1[n/2+1]!=str2[n/2+1]);for(int i=1;i<=n/2;i++){memset(num,0,sizeof(num));num[str1[i]-'a']++;num[str2[i]-'a']++;num[str1[n-i+1]-'a']++;num[str2[n-i+1]-'a']++;int kind=0;bool flag=false;for(int j=0;j<26;j++){if(num[j]){kind++;//統(tǒng)計(jì)字符種類if(num[j]&1)flag=true;}}if(kind==2&&flag)ans++;else if(kind==3)//看串1中兩字符是否相等ans+=(str1[i]==str1[n-i+1])+1;else if(kind==4)ans+=2;}cout<<ans<<endl;return 0; }?
總結(jié)
以上是生活随笔為你收集整理的Two Strings Swaps(CF-1006D)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Apocalypse Someday(P
- 下一篇: Linux 日志系统