1. 程式人生 > 其它 >「ABC221」B - typo 題解

「ABC221」B - 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, print No.

  • 如果可以通過在問題陳述中最多執行一次操作使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;
}