hdu_4357_找字符串规律

String change

In this problem you will receive two strings S1 and S2 that contain only lowercase letters.
Each time you can swap any two characters of S1. After swap,both of the two letters will increase their value by one. If the previous letter is ‘z’,it will become ‘a’ after being swapped.
That is to say ,”a” becomes “b”,”b” becomes “c”…..”z” becomes “a” and so on.
You can do the change operation in S1 as many times as you want.
Please tell us whether you can change S1 to S2 after some operations or not.
Input
There are several cases.The first line of the input is a single integer T (T <= 41) which is the number of test cases.Then comes the T test cases .

For each case,the first line is S1,the second line is S2.S1 has the same length as S2 and the length of the string is between 2 and 60.
Output
For each case,output “Case #X: ” first, X is the case number starting from 1.If it is possible change S1 to S2 output “YES”,otherwise output “NO”.
Sample Input
3
ab
ba

bac
ddb

aaabb
cbccd
Sample Output
Case #1: NO
Case #2: YES
Case #3: YES
Source
2012 Multi-University Training Contest 6

题意:
给你字符串 s1 ,s2 对s1任选两个字符交换位置并且交换后字符+1;经过多少次换能得到s2字符串,就输出YES;
思路:
自己写几个,二是特殊情况

#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#include<algorithm>
#include<cmath>
typedef long long ll;
using namespace  std;
int main()
{
    int t,i,j;
    string s1,s2;
    while(cin>>t)
    {
        for(int c=1;c<=t;c++)
        {
           cin>>s1>>s2;
           printf("Case #%d: ",c);
           if(s1==s2)
           {
               puts("YES");
               continue;
           }
           int len1=s1.length(),len2=s2.length();
           if(len1!=len2)
           {
               puts("NO");
               continue;
           }
           if(len1==2)
           {
               int f=0;
               for(i=1;i<=26;i++)
               {
                   char st=s1[0];
                   s1[0]=s1[1]+1;
                   s1[1]=st+1;
                   if(s1[0]>'z') s1[0]='a';
                   if(s1[1]>'z') s1[1]='a';
                   if(s1==s2)
                   {
                       f=1;
                       break;
                   }
               }
               if(f)
               puts("YES");
               else puts("NO");
               continue;
           }
           else
           {
               ll ans=0;
               for(i=0;i<len1;i++)
               {
                   ans+=s2[i]-s1[i]-'0';
               }
               if(ans&1)
                puts("NO");
               else
                puts("YES");
               continue;
           }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值