心如止水
Je n'ai pas le temps
posts - 400,comments - 130,trackbacks - 0

题目大意:Palindrome的定义是,一个字符串从左向右和从右向左读是一样的;Mirrored string的定义是,一个字符串左右对称;Mirrored palindrome就是既palindrome又mirrored的字符串。对称的关系表题目中已给出。

以下是我的代码:
#include<stdio.h>
#include
<string.h>
const char one[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
const char two[]="A   3 HIL JM O   2TUVWXY51SE Z 8 ";
const long len=35;
bool Palindrome(const char *s)
{
    
long begin=0,end=strlen(s)-1;
    
while(begin<=end)
    
{
       
if(s[begin]==s[end])
       
{
          begin
++;
          end
--;
       }

       
else
         
return false;
    }

    
return true;
}

long pos(char ch)
{
    
for(long i=0;i<len;i++)
      
if(one[i]==ch)
        
return i;
    
return len;
}

bool Mirror(const char *s)
{
    
long begin=0,end=strlen(s)-1;
    
while(begin<=end)
    
{
       
long tmp=pos(s[begin]);
       
if(s[end]==two[tmp])
       
{
          begin
++;
          end
--;
       }

       
else
         
return false;
    }

    
return true;
}

int main()
{
    
char str[1000];
    
while(gets(str)!=0)
    
{
       
bool a=false,b=false;
       a
=Palindrome(str);
       b
=Mirror(str);
       
if(a&&b)
         printf(
"%s -- is a mirrored palindrome.\n",str);
       
else if(a)
         printf(
"%s -- is a regular palindrome.\n",str);
       
else if(b)
         printf(
"%s -- is a mirrored string.\n",str);
       
else
         printf(
"%s -- is not a palindrome.\n",str);
       putchar(
'\n');
    }

return 0;
}

posted on 2010-01-06 18:02 lee1r 阅读(1801) 评论(6)  编辑 收藏 引用 所属分类: 题目分类:字符串处理

FeedBack:
# re: UVa 401 Palindromes
2010-02-07 21:31 | Climber.pI
貌似这个是WA的  回复  更多评论
  
# re: UVa 401 Palindromes
2010-02-07 21:59 | Lee1R
@Climber.pI
呵呵,我贴上来的都是AC了的,不用怀疑。
401 634 7663819 2010-01-03 14:06:26 0.012
  回复  更多评论
  
# re: UVa 401 Palindromes
2010-02-20 21:51 | songyy
其实……要是直接复制粘贴这个的确是WA……因为可能是因为帖子的原因,one[] 和 two[]字符对不上,需要读者自己调整下。

另外,这个代码的巧妙处理避开了这道题目最绕人的地方:单个字符的输入。
WA的同学看这里:http://online-judge.uva.es/board//viewtopic.php?f=5&t=5863&sid=91aaddded4ff3586f1e90addd4dd460a

或者我简要说下:
单字符情况,比如,我输入 E,只能判定palindrome,而不是mirror;
如果是输入 4,也是只能判定palindrome,不是mirror(但是两者原因不同,注意这句话:“A mirrored string is a string for which when each of the elements of the string is changed to its reverse (if it has a reverse) and the string is read backwards the result is the same as the original string.”),重点强调“(if it has a reverse) ”
所以如果既是palindrome,也是mirror,只能是A,O之类的情况(自身mirror自身)  回复  更多评论
  
# re: UVa 401 Palindromes
2010-02-20 23:15 | Lee1R
@songyy
当时写程序的时候似乎没有考虑那么多关于单个字符的问题,只是按照自己的想法(当然也是出于简单考虑),开了两个数组,用函数的思想让字符之间形成一一对应的关系,方便判断。
  回复  更多评论
  
# re: UVa 401 Palindromes
2010-03-05 12:18 | binyan
可否加你qq?  回复  更多评论
  
# re: UVa 401 Palindromes
2010-03-05 18:46 | Lee1R
@binyan
QQ120501168
加的时候让我知道你是哪位  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理