zoukankan      html  css  js  c++  java
  • 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";
    }
    View Code
  • 相关阅读:
    js 删除字符串中所有空格
    jquery easyui datagrid 设置设置在选中的所有行中只选择第一行
    编译Linux内核时出现错误gcc: error: elf_i386: No such file or directory
    AD9打印丝印层
    s3c2410 board.c分析
    2010.03 u-boot--Makefile完全分析
    mini6410移植--uboot移植(2)
    mini6410移植--uboot移植(1)
    uboot之uboot.lds文件分析
    Linux启动过程
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/7440275.html
Copyright © 2011-2022 走看看