http://acm.hdu.edu.cn/showproblem.php?pid=4357
推理题 要想在比赛中准确推出来 还真难 看了解析才懂得
以下转自出题人解析:
当字符串长度为2时:直接模拟即可(最多26次)。
当字符串长度大于2时:
1. 定义字符串的奇偶性为该字符串所有字符之和的奇偶性。
2. 因为每次变化操作字符串的字符和共增加了2,所以当字符串的奇偶
性不同时答案一定为N O。
3. 当字符串的奇偶性相同时可证明答案一定为Y ES,证明如下:
对于任意3个位置的字符(x1 , x2 , x3 ),可进行如下变化:
(x1 , x2 , x3 ) → (x1 , x3 + 1, x2 + 1) → (x2 + 2, x3 + 1, x1 + 1) → (x2 +
2, x1 + 2, x3 + 2) → (x1 + 3, x2 + 3, x3 + 2).(记为变化方式1)。
在变化方式1的基础上,再将这3个位置的字符两两各交换12次,即
得(x1 + 3 + 12 + 12, x2 + 3 + 12 + 12, x3 + 2 + 12 + 12)即(x1 + 27, x2 +
27, x3 + 26)(记为变化方式2)。
又 一 共 只 有26个 小 写 字 母 , 所 以 变 化 方 式 2 等 同 于(x1 , x2 , x3 ) →
(x1 + 1, x2 + 1, x3 )。即保证了不改变字符位置顺序的情况下对两
个字符进行了+1操作。
所以每次我们只需任选和S2 该位置不相同的两个位置的字符进行变化
方式2的操作,那么最后要么S1 完全变为S2 ,要么S1 和S2 只有一个位
置的字符不相同,对于只有一个位置字符不相同的情况,因为字符串
奇偶性相同,所以可进行变化1的操作使得该位置的差值平摊到另外
两个位置,再对这另外两个位置进行变化2的操作即得到了S2
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map> #include<stack> #include<cmath> #define LL long long using namespace std; const int N=105; char a[N]; char b[N]; bool func() { for(int i=0;i<26;++i) { if(a[0]==b[0]&&a[1]==b[1]) return true; swap(a[0],a[1]); a[0]=(a[0]=='z')?'a':a[0]+1; a[1]=(a[1]=='z')?'a':a[1]+1; } return false; } int main() { //freopen("data.txt","r",stdin); int T; scanf("%d",&T); for(int ca=1;ca<=T;++ca) { getchar(); gets(a); gets(b); printf("Case #%d: ",ca); int n=strlen(a); if(n==2) { if(func()) printf("YES\n"); else printf("NO\n"); continue; } int k1=0,k2=0; for(int i=0;i<n;++i) { k1=k1+a[i]; k2=k2+b[i]; } if(k1%2==k2%2) { printf("YES\n"); }else { printf("NO\n"); } } return 0; }