Advertisement

[规律] hdu 4357 string change

阅读量:
复制代码
 /**

    
 [规律] hdu 4357
    
 from hdoj人人
    
 当字符串长度为2时,直接模拟,最多26次
    
 否则。。
    
 1,定义字符串的奇偶性为该字符串所有字符之和的奇偶性
    
 2,当字符串的奇偶性不同时肯定为NO
    
 3,当字符串的奇偶性相同时一定是YES ,证明如下
    
 对于任意三个位置的字符(x,y,z),可进行变化:
    
 (x,y,z) (x,z+1,y+1) (y+2,z+1,x+1) (y+2,x+2,z+2) (x+3,y+3,z+2)
    
 记为变化方式1;
    
 在变化1的基础上,再将这3个位置的字符两两各交换12次,即得
    
 (x + 3 + 12 + 12,y + 3 + 12 + 12,z + 2 + 12 + 12),即(x+27,y+27,z+26)
    
 记为变化方式2;(x+1,y+1,z),
    
 即不改变字符位置的情况下对两个字符经行了+1操作。
    
 那么,对s1若干操作后肯定得到s2或和s2仅有一个字母不同。
    
 后者时,又因奇偶性相同,可进行1变化将该位置的差值平摊到另两个位置。
    
 */
    
 #include <stdio.h>
    
 #include <string.h>
    
 #include <algorithm>
    
 #define N 66
    
 int main()
    
 {
    
     char s[N],ss[N];
    
     int t,a,b,cas = 0,i;
    
     scanf("%d",&t);
    
     while(t--)
    
     {
    
     scanf("%s%s",s,ss);
    
     if(s[2] == 0)
    
     {
    
         for(i = 0; i < 26; ++i)
    
         {
    
             std::swap(s[0],s[1]);
    
             if(++s[0] > 'z')
    
                 s[0] = 'a';
    
             if(++s[1] > 'z')
    
                 s[1] = 'a';
    
             if(s[0] == ss[0] && ss[1] == s[1])
    
                 break;
    
         }
    
         printf(i == 26 ? "Case #%d: NO\n":"Case #%d: YES\n",++cas);
    
         continue;
    
     }
    
     for(i = 0,a = 0,b = 0; s[i]; ++i)
    
         a += (int)s[i],b += (int)ss[i];
    
     printf(a%2 == b%2 ? "Case #%d: YES\n":"Case #%d: NO\n",++cas);
    
     }
    
     return 0;
    
 }
    
    
    
    
    代码解读

全部评论 (0)

还没有任何评论哟~