zoukankan      html  css  js  c++  java
  • 401 Palindromes

      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.

    Character Reverse Character Reverse Character Reverse
    A A M M Y Y
    B N Z 5
    C O O 1 1
    D P 2 S
    E 3 Q 3 E
    F R 4
    G S 2 5 Z
    H H T T 6
    I I U U 7
    J L V V 8 8
    K W W 9
    L J X X


    Note that O (zero) and 0 (the letter) are considered the same character and therefore ONLYthe 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.

    STRING CRITERIA
    " -- 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.
    

    注解:在这里要特别注意,当只有一个元素的时候,就会出现Is_Mirrored()函数里面的循环直接忽视的结果,那就需要修改一下里面的循环条件:

    i < length/2 修改为 i < (length+1)/2 ; 这样就不会将length = 1 的情况漏掉 ; 
    
    #include <iostream> 
    #include <string> 
    
    using namespace std ; 
    
    string alpha_string = "A---3--HIL-JM-O---2TUVWXY5" ; 
    string number_string = "1SE-Z--8-" ; 
    
    bool Is_Palindrome(string word) { 
    	int length = word.length() ; 
    
    	for (int i = 0 ; i < length/2 ; ++ i) { 
    		if (word.at(i) != word.at(length-1-i)) { 
    			return false ; 
    		} 
    	} 
    
    	return true ; 
    }  
    
    bool Is_Mirrored(string word) { 
    	int length = word.length() ; 
    	for (int i = 0 ; i < (length+1)/2 ; ++ i) {  
    		int index = 0 , key = 0 ; 
    		if (word.at(length-1-i) >= 'A') { 
    			key = 1 ; 
    			index = word.at(length-1-i)-'A' ; 
    		} 
    		else { 
    			key = 0 ; 
    			index = word.at(length-1-i)-'1' ; 
    		} 
    		if (key) { 
    			if (word.at(i) != alpha_string.at(index)) {  
    
    				return false ; 
    			} 
    		} 
    		else { 
    			if (word.at(i) != number_string.at(index)) {  
    
    				return false ; 
    			} 
    		}   
    	} 
    	return true ; 
    } 
    
    int main() { 
    	string word ;  
    
    	while(cin >> word) {  
    		
    		cout << word ; 
    		if (Is_Palindrome(word) && Is_Mirrored(word)) { 
    			cout << " -- is a mirrored palindrome." << endl ; 
    		} 
    		else if (Is_Mirrored(word)) { 
    			cout << " -- is a mirrored string." << endl ; 
    		} 
    		else if (Is_Palindrome(word)) { 
    			cout << " -- is a regular palindrome." << endl ; 
    		} 
    		else { 
    			cout << " -- is not a palindrome."  << endl ; 
    		} 
    		cout << endl ; 
    	} 
    
    	return 0 ; 
    } 
    
    bool Is_Palindrome(string word) { 
    	int length = word.length() ; 
    	int begin = 0 , end = length-1 ; 
    
    	while (begin <= end) { 
    		if (word.at(begin) != word.at(end)) { 
    			return false ; 
    		} 
    		begin ++ ; 
    		end -- ; 
    	} 
    
    	return true ; 
    }  
    
    bool Is_Mirrored(string word) { 
    	int begin = 0 , end = word.length()-1 ; 
    	
    	while(begin <= end) { 
    		int index = 0 , key = 0 ; 
    		if (word.at(end) >= 'A') { 
    			key = 1 ; 
    			index = word.at(end)-'A' ; 
    		} 
    		else { 
    			key = 0 ; 
    			index = word.at(end)-'1' ; 
    		} 
    		if (key) { 
    			if (word.at(begin) != alpha_string.at(index)) { 
    				return false ; 
    			}  
    		} 
    		else { 
    			if (word.at(begin) != number_string.at(index)) { 
    				return false ; 
    			} 
    		} 
    		begin ++ ; 
    		end -- ; 
    	} 
    	return true ; 
    } 
    

  • 相关阅读:
    使用ZooKeeper实现Java跨JVM的分布式锁
    基于ZooKeeper的分布式锁和队列
    activiti数据库表结构剖析
    visualvm监控jvm及远程jvm监控方法
    使用visualvm 远程监控 JVM
    java jprofile
    Linux服务器上监控网络带宽的18个常用命令
    Redis-sentinel哨兵模式集群方案配置
    电容的去耦半径
    DC-DC BUCK电源芯片的基本原理和组成
  • 原文地址:https://www.cnblogs.com/diyingyun/p/2174766.html
Copyright © 2011-2022 走看看