Codeforces 186A. Comparing Strings

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".

先看长度等不等,不等直接no

等的情况下观察两个字符串有几位是不一样的,不为2就是no,为2的情况下换一下再比较试试看

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

string s1,s2;

int main(){
  // freopen("test.in","r",stdin);
  cin >> s1 >> s2;
  int len = s1.length();
  if (len != s2.length()){
    cout << "NO"; return 0;
  }
  int total = 0,pos1 = 0,pos2 = 0;
  for (int i=0;i<len;i++){
    if (s1[i] != s2[i]){
      total ++;
      if (total == 1){
        pos1 = i;
      }
      else {
        pos2 = i;
      }
    }
  }
  if (total == 2 || total == 0){
    swap(s1[pos1],s1[pos2]);
    if (s1 == s2){
      cout << "YES";
    }
    else
      cout << "NO";
  }
  else
    cout << "NO";
}

时间: 2025-01-31 15:07:46

Codeforces 186A. Comparing Strings的相关文章

[2016-04-26][codeforces][665C - Simple Strings]

时间:2016-04-26 10:11:21 星期二 题目编号:[2016-04-26][codeforces][665C - Simple Strings] 题目大意:给定一个字符串,问最少需要更改多少字符,使得相邻的字母都不一样,输出更改后的字符串 分析: 贪心,从左往右扫一遍数组,更改相同的元素即可 遇到的问题: 注意,相同元素必须更改后面那个元素才是最优的答案. 比如 aaa 这种情况只能更改中间那个 #include<iostream> #include<string>

codeforces 616A Comparing Two Long Integers

A. Comparing Two Long Integers You are given two very long integers a, b (leading zeroes are allowed). You should check what number a or b is greater or determine that they are equal. The input size is very large so don't use the reading of symbols o

Codeforces 665C - Simple Strings

665C - Simple Strings 思路:贪心.为了保证最小改变着次数,在改变每个相邻相同的字符时,保证改变后的字符和后面也不相同. 代码: #include<bits/stdc++.h> using namespace std; string s; int main() { ios::sync_with_stdio(false); cin.tie(0); cin>>s; for(int i=1;i<s.size()-1;i++) { if(s[i]==s[i-1])

Swift学习笔记-字符串和字符(Strings and Characters)-比较字符串 (Comparing Strings)

Swift 提供了三种方式来比较文本值:字符串字符相等.前缀相等和后缀相等. 字符串/字符相等 (String and Character Equality) 字符串/字符可以用等于操作符(==)和不等于操作符(!=),详细描述在比较运算符: let quotation = "We're a lot alike, you and I." let sameQuotation = "We're a lot alike, you and I." if quotation

CodeForces 559B Equivalent Strings

Portal: http://codeforces.com/problemset/problem/559/B 年轻时第一次cf的D题,当时直接写了个递归结果爆炸 做法大概是递归时候按字典序乱搞一下 然后就nlogn水过了 1 #include<iostream> 2 #include<algorithm> 3 #include<set> 4 #include<cstdio> 5 #include<cstdlib> 6 #include<cm

CodeForces - 985F Isomorphic Strings

假如两个区间的26的字母出现的位置集合分别是 A1,B1,A2,B2,....., 我们再能找到一个排列p[] 使得 A[i] = B[p[i]] ,那么就可以成功映射了. 显然集合可以直接hash,排序一下hash值就可以判断是否匹配了.... 虽然不知道为什么要卡19260817,但反正我是坚决不写双hash的,最后随便换了一个大模数(甚至都不是质数2333)然后就过了... Discription You are given a string s of length n consistin

[Codeforces Round #438][Codeforces 868D. Huge Strings]

题目链接:868D - Huge Strings 题目大意:有\(n\)个字符串,\(m\)次操作,每次操作把两个字符串拼在一起,并询问这个新串的价值.定义一个新串的价值\(k\)为:最大的\(k\),使得这个新串包含所有长度为\(k\)的01串(这样的字符串有\(2^k\)个) 题解:首先来证明对于任何的串,这个\(k\)的值不会超过9 若\(k=10\),由所有字符串的长度总和不超过100,因此在初始的\(n\)个串里,互不相同的长度为\(k\)的子串不超过100个.又由于每次连接最多能产生

Codeforces - 559B - Equivalent Strings - 分治

http://codeforces.com/problemset/problem/559/B 这个题目,分治就好了,每次偶数层可以多一种判断方式,判断它的时间就是logn的(吧),注意奇数层并不是直接退出!题目给了另一种相等的条件. #include<bits/stdc++.h> using namespace std; #define ll long long char s[200005]; char t[200005]; bool isequal(char *sb,int slen,cha

Codeforces 544E Remembering Strings 状压dp

题目链接 题意: 给定n个长度均为m的字符串 下面n行给出字符串 下面n*m的矩阵表示把对应的字母修改成其他字母的花费. 问: 对于一个字符串,若它是easy to remembering 当 它存在一个字母,使得这个字母在这一列是独一无二的. 要使得n个字符串都是easy to remembering 的最小花费. 第一个样例是把第一列的4个a中3个a修改成别的字母,所以花费为3. 思路: 显然是个状压dp,但需要一点转化. 首先得到一个结论: 对于某一列,设这一列的字母是 a,a,b,b,a