A. Comparing Strings

来源:互联网 发布:网络布线桥架 编辑:程序博客网 时间:2024/06/07 16:31

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Some dwarves that are finishing the StUDY (State University for Dwarven Youngsters) Bachelor courses, have been told "no genome, no degree". That means that all dwarves should write a thesis on genome. Dwarven genome is far from simple. It is represented by a string that consists of lowercase Latin letters.

Dwarf Misha has already chosen the subject for his thesis: determining by two dwarven genomes, whether they belong to the same race. Two dwarves belong to the same race if we can swap two characters in the first dwarf's genome and get the second dwarf's genome as a result. Help Dwarf Misha and find out whether two gnomes belong to the same race or not.

Input

The first line contains the first dwarf's genome: a non-empty string, consisting of lowercase Latin letters.

The second line contains the second dwarf's genome: a non-empty string, consisting of lowercase Latin letters.

The number of letters in each genome doesn't exceed 105. It is guaranteed that the strings that correspond to the genomes are different. The given genomes may have different length.

Output

Print "YES", if the dwarves belong to the same race. Otherwise, print "NO".

Sample test(s)
input
abba
output
YES
input
aaab
output
NO
Note

  • First example: you can simply swap two letters in string "ab". So we get "ba".
  • Second example: we can't change string "aa" into string "ab", because "aa" does not contain letter "b".

解题说明:此题就是判断一个字符串能否通过调整其中两个字母的顺序变成另外一个字符串,可以先统计原始两个字符串中不一样的地方,看看是否只有两个位置字母不同,然后分别对这两个字符串中的字母进行从小到大排序,看看是不是能变成一样的,如果原串确实只有两个位置不同而且排序后两个串相同,那么就证明能够通过变换得到。


#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <string>#include<set>#include <algorithm>using namespace std;int main() {    string a, b;int i,c=0;    cin >> a >> b;    for (i = 0; i < a.size() && i < b.size(); i++) {        if(a[i] != b[i]){c++;}    }    sort(a.begin(), a.end());    sort(b.begin(), b.end());if(a==b&&c==2){printf("YES\n");}else{printf("NO\n");}return 0;}