「ABC221B」typo 题解
B - typo
Time Limit: \(2\; sec\) / Memory Limit: \(1024\; MB\)
Score : \(200\; points\)
Problem Statement|题目描述
- You are given two strings \(S\) and \(T\). Determine whether it is possible to make \(S\) and \(T\) equal by doing the following operation at most once:
choose two adjacent characters in \(S\) and swap them.
- 将为您提供两个字符串 \(S\) 和 \(T\) 。请确定是否可以通过最多执行一次以下操作使 \(S\) 和 \(T\) 相等:
选择 \(S\) 中的两个相邻字符并交换它们。
-
Note that it is allowed to choose not to do the operation.
-
需要注意的是,你可以选择不执行该操作。
Constraints|数据范围
-
Each of \(S\) and \(T\) is a string of length between \(2\) and \(100\) (inclusive) consisting of lowercase English letters.
-
\(S\) and \(T\) have the same length.
-
\(S\) 和 \(T\) 各是一个长度介于 \(2\) 和 \(100\)(含)之间的字符串,由小写英文字母组成。
-
\(S\) 和 \(T\) 的长度相同。
Input|输入
-
Input is given from Standard Input in the following format:
S
T
-
输入为以下格式的标准输入:
S
T
Output|输出
-
If it is possible to make S and T equal by doing the operation in Problem Statement at most once, print
Yes
; otherwise, printNo
. -
如果可以通过在问题陈述中最多执行一次操作使S和T相等,请打印
Yes
;否则,请打印No
。
Sample Input 1 |样例输入 1
abc
acb
Sample Output 1 |样例输出 1
Yes
- You can swap the \(2\)-nd and \(3\)-rd characters of \(S\) to make \(S\) and \(T\) equal.
- 可以交换 \(S\) 的第 \(2\) 和第 \(3\) 个字符,使 \(S\) 和 \(T\) 相等。
Sample Input 2 |样例输入 2
aabb
bbaa
Sample Output 2 |样例输出 2
No
- There is no way to do the operation to make \(S\) and \(T\) equal.
- 无法进行使 \(S\) 和 \(T\) 相等的操作。
Sample Input 3 |样例输入 3
abcde
abcde
Sample Output 3 |样例输出 3
Yes
- \(S\) and \(T\) are already equal.
- \(S\) 和 \(T\) 已经相等了。
分析
本来就是个简单判断,但我写得挺复杂的。
应该不用解释了:
#include<bits/stdc++.h>
const int maxn=110;
char s[maxn],t[maxn];
inline void myswap(int x,int y){
int temp=s[x];
s[x]=s[y];
s[y]=temp;
}
int main(){
scanf("%s%s",s+1,t+1);
int cnt=0;
for(int i=1;i<=strlen(s+1);i++){
if(cnt>1){
printf("No");
return 0;
}
if(s[i]!=t[i]){
if(s[i-1]!=t[i-1]){
if(s[i]==t[i-1]&&t[i]==s[i-1]){
myswap(i,i-1);
cnt++;
}else{
printf("No");
return 0;
}
}else if(s[i+1]!=t[i+1]){
if(s[i]==t[i+1]&&t[i]==s[i+1]){
myswap(i,i+1);
cnt++;
}else{
printf("No");
return 0;
}
}else{
printf("No");
return 0;
}
}
}
if(cnt>1)printf("No");
else printf("Yes");
return 0;
}
$$-----CONTINUE-----$$
< last 「ABC221A」Seismic magnitude scales 题解
> next 「ABC221C」Select Mul 题解