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;
}