本文主要是介绍UVA401 Palindromes【字符串处理】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
输入一个字符串,判断它是否为回文串以及镜像串。输入字符串保证不含数字0。所谓回文串,就是反转以后和原串相同,如abba和madam。所谓镜像串,就是左右镜像之后和原串相同,如2S和3AIAE。注意,并不是每个字符在镜像之后都能得到一个合法字符。在本题中,每个字符的镜像如图3-3所示(空白项表示该字符镜像后不能得到一个合法字符)。
输入的每行包含一个字符串(保证只有上述字符。不含空白字符),判断它是否为回文串和镜像串(共4种组合)。每组数据之后输出一个空行。
样例输入
NOTAPALINDROME
ISAPALINILAPASI
2A3MEAS
ATOYOTA
样例输出
NOTAPALINDROME -- is not a palindrome.
ISAPALINILAPASI -- is a regular palindrome.
2A3MEAS -- is a mirrored string.
ATOYOTA -- is a mirrored palindrome.
使用C++ STL的map进行操作,具体看程序
#include<iostream>
#include<map>
#include<string>using namespace std;map<char,char>d;bool palindrome(string s)
{int i,j;for(i=0,j=s.length()-1;i<j&&s[i]==s[j];i++,j--);if(i<j)return false;return true;
}bool mirrored(string s)
{string p=s;for(int i=0;i<s.length();i++)if(d.count(s[i]))s[i]=d[s[i]];elsereturn false;for(int i=0;i<s.length();i++)if(s[i]!=p[s.length()-i-1])return false;return true;
}int main()
{d['A']='A',d['E']='3',d['H']='H',d['I']='I',d['J']='L',d['L']='J';d['M']='M',d['O']='O',d['S']='2',d['T']='T',d['U']='U',d['V']='V';d['W']='W',d['X']='X',d['Y']='Y',d['Z']='5',d['1']='1',d['2']='S';d['3']='E',d['5']='Z',d['8']='8';string s;while(cin>>s){string p=s;for(int i=0;i<s.length();i++)if(s[i]=='0')s[i]='O';bool flag1=palindrome(s),flag2=mirrored(s);if(flag1){if(flag2)cout<<p<<" -- is a mirrored palindrome."<<endl<<endl;elsecout<<p<<" -- is a regular palindrome."<<endl<<endl;}else{if(flag2)cout<<p<<" -- is a mirrored string."<<endl<<endl;elsecout<<p<<" -- is not a palindrome."<<endl<<endl;} }return 0;
}
这篇关于UVA401 Palindromes【字符串处理】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!