uva 401 Palindromes

本文介绍了一种算法,该算法能够判断一个字符串是否为回文或镜像字符串,并进一步判断是否同时满足两种条件。通过示例代码展示了如何使用C++实现这一功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

                                        Palindromes                                                              

A regular palindrome is a string of numbers or letters that is the same  forward as backward.  For example, the string"ABCDEDCBA" is a palindrome because it is the same when the string is read from left to  right as when the string is read from right to left.


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.   For example, the string"3AIAE" is a mirrored string because "A"and "I" are their own reverses, and"3" and"E"are each others' reverses.


A mirrored palindrome is a string that meets the criteria of a regular  palindrome and the criteria of a mirrored string.  The string"ATOYOTA"is a mirrored palindrome because if the string is read backwards, the  string is the same as the original and because if each of the  characters is replaced by its reverse and the result is read backwards,  the result is the same as the original string.  Of course,"A","T","O", and"Y" are all their own reverses.


A list of all valid characters and their reverses is as follows.


CharacterReverseCharacterReverseCharacterReverse
AAMMYY
B N Z5
C OO11
D P 2S
E3Q 3E
F R 4 
G S25Z
HHTT6 
IIUU7 
JLVV88
K WW9 
LJXX  


Note that O (zero) and 0 (the letter) are considered the same  character and thereforeONLY the letter "0" is a valid character.

Input

Input consists of strings (one per line) each of which will consist of  one to twenty valid characters.  There will be no invalid characters in  any of the strings.  Your program should read to the end of file.

Output

For each input string, you should print the string starting in column 1  immediately followed by exactly one of the following strings.


STRINGCRITERIA
" -- is not a palindrome."if the string is not a palindrome  and is not a mirrored string
" -- is a regular palindrome."if the string is a palindrome and  is not a mirrored string
" -- is a mirrored string."if the string is not a palindrome  and is a mirrored string
" -- is a mirrored palindrome."if the string is a palindrome and  is a mirrored string

Note that the output line is to include the -'s and spacing exactly as shown in the table above and demonstrated in the Sample Output below. 

In addition, after each output line, you must print an empty line.

Sample Input

NOTAPALINDROME 
ISAPALINILAPASI 
2A3MEAS 
ATOYOTA

Sample Output

NOTAPALINDROME -- is not a palindrome.
 
ISAPALINILAPASI -- is a regular palindrome.
 
2A3MEAS -- is a mirrored string.
 
ATOYOTA -- is a mirrored palindrome.



Miguel Revilla 2001-04-16

 

题目大意:如果是回文的字符串,那么就是"is a regular palindrome",如果是镜像对称的字符串,那么就是"is a mirrored string ",如果既是回文的又是镜像对称的字符串则是"is a mirrored palindrome";  既不是回文的也不是镜像对称的就是"is not a palindrome".

思路:模拟..

 

 

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
using namespace std;
char ch[25];
map<string,string>mapTmp;
int isPalindrome()
{
     int len=strlen(ch),half;
     if(len&1)
        half=len/2;
     else
        half=(len-1)/2;
     for(int i=0;i<=half;i++)
     {
        if(ch[i]!=ch[len-1-i]) //一旦出现两边不是同一个字母,利马返回失败   
            return 0;
        
     } 
     return 1;
}
int isMirrored()
{
    int len=strlen(ch),half;
     if(len&1)
        half=len/2;
     else
        half=(len-1)/2;
     for(int i=0;i<=half;i++)
     {
        string tmp;
        tmp.assign(ch,i,1); //在这用assign,是很把字符放到string变量中 
        string tmp1=mapTmp[tmp];
        string tmp2;
        tmp2.assign(ch,len-1-i,1); 
        //cout<<"tmp1 tmp2 "<<tmp1<<"  "<<tmp2<<endl;  
        if(tmp1.compare(tmp2))//一旦出现两边不是镜像的,利马返回失败       
            return 0;
        
     } 
     return 1;
}

int main()
{
    mapTmp["A"]="A";
    mapTmp["B"]="";
    mapTmp["C"]="";
    mapTmp["D"]="";
    mapTmp["E"]="3";
    mapTmp["F"]="";
    mapTmp["G"]="";
    mapTmp["H"]="H";
    mapTmp["I"]="I";
    mapTmp["J"]="L";
    mapTmp["K"]="";
    mapTmp["L"]="J";
    mapTmp["M"]="M";
    mapTmp["N"]="";
    mapTmp["O"]="O";
    mapTmp["P"]="";
    mapTmp["Q"]="";
    mapTmp["R"]="";
    mapTmp["S"]="2";
    mapTmp["T"]="T";
    mapTmp["U"]="U";
    mapTmp["V"]="V";
    mapTmp["W"]="W";
    mapTmp["X"]="X";
    mapTmp["Y"]="Y";
    mapTmp["Z"]="5";
    mapTmp["1"]="1";
    mapTmp["2"]="S";
    mapTmp["3"]="E";
    mapTmp["4"]="";
    mapTmp["5"]="Z";
    mapTmp["6"]="";
    mapTmp["7"]="";
    mapTmp["8"]="8"; 
    mapTmp["9"]="";
    while(~scanf("%s",ch))
    {
      int isP=isPalindrome();
      int isM=isMirrored(); 
      if(isP==0&&isM==0)//都不是
      {
         printf("%s -- is not a palindrome.\n\n",ch);                       
                               
      }
      else if(isP==0&&isM==1)
      {
          printf("%s -- is a mirrored string.\n\n",ch); 
      }
      else if(isP==1&&isM==0)
      {
         printf("%s -- is a regular palindrome.\n\n",ch);  
      }
      else if(isP==1&&isM==1) 
      {
        printf("%s -- is a mirrored palindrome.\n\n",ch);     
      }
    }
   return 0; 
} 


 

 

      

    C++ string类的成员函数,用于拷贝、赋值操作,它们允许我们顺次地把一个string 对象的


  部分内容拷贝到另一个string 对象上。


  函数原型:


  string &operator=(const string &s);把字符串s赋给当前字符串


  string &assign(const char *s);用c类型字符串s赋值


  string &assign(const char *s,int n);用c字符串s开始的n个字符赋值


  string &assign(const string &s);把字符串s赋给当前字符串


  string &assign(int n,char c);用n个字符c赋值给当前字符串


  string &assign(const string &s,int start,int n);把字符串s中从start开始的n个字符赋给当前字符串


  string &assign(const_iterator first,const_itertor last);把first和last迭代器之间的部分赋给字符串


 

 

上面的是用map写的,感觉其实大才小用.下面用的是字符串数组,其实也一样的.也是映射了,只不过,上面的空串我只好用一个空格来代替了

 

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
using namespace std;
char ch[25];
char mapTmp[300];
int isPalindrome()
{
     int len=strlen(ch),half;
     if(len&1)
        half=len/2;
     else
        half=(len-1)/2;
     for(int i=0;i<=half;i++)
     {
        if(ch[i]!=ch[len-1-i]) //一旦出现两边不是同一个字母,利马返回失败   
            return 0;
        
     } 
     return 1;
}
int isMirrored()
{
    int len=strlen(ch),half;
     if(len&1)
        half=len/2;
     else
        half=(len-1)/2;
     for(int i=0;i<=half;i++)
     {
        
        char tmp1=mapTmp[ch[i]]; 
        if(tmp1!=ch[len-1-i]) //一旦出现两边不是镜像的,利马返回失败     
            return 0;    
     } 
     return 1;
}

int main()
{
    mapTmp['A']='A';
    mapTmp['B']=' ';
    mapTmp['C']=' ';
    mapTmp['D']=' ';
    mapTmp['E']='3';
    mapTmp['F']=' ';
    mapTmp['G']=' ';
    mapTmp['H']='H';
    mapTmp['I']='I';
    mapTmp['J']='L';
    mapTmp['K']=' ';
    mapTmp['L']='J';
    mapTmp['M']='M';
    mapTmp['N']=' ';
    mapTmp['O']='O';
    mapTmp['P']=' ';
    mapTmp['Q']=' ';
    mapTmp['R']=' ';
    mapTmp['S']='2';
    mapTmp['T']='T';
    mapTmp['U']='U';
    mapTmp['V']='V';
    mapTmp['W']='W';
    mapTmp['X']='X';
    mapTmp['Y']='Y';
    mapTmp['Z']='5';
    mapTmp['1']='1';
    mapTmp['2']='S';
    mapTmp['3']='E';
    mapTmp['4']=' ';
    mapTmp['5']='Z';
    mapTmp['6']=' ';
    mapTmp['7']=' ';
    mapTmp['8']='8'; 
    mapTmp['9']=' ';
    while(~scanf("%s",ch))
    {
      int isP=isPalindrome();
      int isM=isMirrored(); 
      if(isP==0&&isM==0)//都不是
      {
         printf("%s -- is not a palindrome.\n\n",ch);                       
                               
      }
      else if(isP==0&&isM==1)
      {
          printf("%s -- is a mirrored string.\n\n",ch); 
      }
      else if(isP==1&&isM==0)
      {
         printf("%s -- is a regular palindrome.\n\n",ch);  
      }
      else if(isP==1&&isM==1) 
      {
        printf("%s -- is a mirrored palindrome.\n\n",ch);     
      }
    }
   return 0; 
} 


 

内容概要:本文介绍了多种开发者工具及其对开发效率的提升作用。首先,介绍了两款集成开发环境(IDE):IntelliJ IDEA 以其智能代码补全、强大的调试工具和项目管理功能适用于Java开发者;VS Code 则凭借轻量级和多种编程语言的插件支持成为前端开发者的常用工具。其次,提到了基于 GPT-4 的智能代码生成工具 Cursor,它通过对话式编程显著提高了开发效率。接着,阐述了版本控制系统 Git 的重要性,包括记录代码修改、分支管理和协作功能。然后,介绍了 Postman 作为 API 全生命周期管理工具,可创建、测试和文档化 API,缩短前后端联调时间。再者,提到 SonarQube 这款代码质量管理工具,能自动扫描代码并检测潜在的质量问题。还介绍了 Docker 容器化工具,通过定义应用的运行环境和依赖,确保环境一致性。最后,提及了线上诊断工具 Arthas 和性能调优工具 JProfiler,分别用于生产环境排障和性能优化。 适合人群:所有希望提高开发效率的程序员,尤其是有一定开发经验的软件工程师和技术团队。 使用场景及目标:①选择合适的 IDE 提升编码速度和代码质量;②利用 AI 编程助手加快开发进程;③通过 Git 实现高效的版本控制和团队协作;④使用 Postman 管理 API 的全生命周期;⑤借助 SonarQube 提高代码质量;⑥采用 Docker 实现环境一致性;⑦运用 Arthas 和 JProfiler 进行线上诊断和性能调优。 阅读建议:根据个人或团队的需求选择适合的工具,深入理解每种工具的功能特点,并在实际开发中不断实践和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值