鏡像字符

Let’s call a string “s-palindrome” if it is symmetric about the middle of the string. For example, the string “oHo” is “s-palindrome”, but the string “aa” is not. The string “aa” is not “s-palindrome”, because the second half of it is not a mirror reflection of the first half.
在這裏插入圖片描述
English alphabet
You are given a string s. Check if the string is “s-palindrome”.

Input
The only line contains the string s (1 ≤ |s| ≤ 1000) which consists of only English letters.

Output
Print “TAK” if the string s is “s-palindrome” and “NIE” otherwise.

Examples
Input
oXoxoXo
Output
TAK
Input
bod
Output
TAK
Input
ER
Output
NIE

思路:把那些鏡像的兩個字符寫出來,然後再去比較就好了
我自己寫的

#include<bits/stdc++.h>
using namespace std;
const int maxx=1e4+10;
char a[maxx];
int main()
{
    std::ios::sync_with_stdio(false);
    cin>>a;
    int len=strlen(a);
    long long int sum=0;
    for(int i=0; i<=len/2; i++)//偶數的時候除了每個比較了一次之外還多比較了一次,奇數的時候每個都比較了一次
    {
        if(a[i]=='b'&&a[len-i-1]=='d')
            sum++;
        else if(a[i]=='d'&&a[len-i-1]=='b')
            sum++;
        else if(a[i]=='o'&&a[len-i-1]=='o')
            sum++;
        else if(a[i]=='p'&&a[len-i-1]=='q')
            sum++;
        else if(a[i]=='q'&&a[len-i-1]=='p')
            sum++;
        else if(a[i]=='v'&&a[len-i-1]=='v')
            sum++;
        else if(a[i]=='w'&&a[len-i-1]=='w')
            sum++;
        else if(a[i]=='x'&&a[len-i-1]=='x')
            sum++;
        else if(a[i]=='A'&&a[len-i-1]=='A')
            sum++;
        else if(a[i]=='H'&&a[len-i-1]=='H')
            sum++;
        else if(a[i]=='I'&&a[len-i-1]=='I')
            sum++;
        else if(a[i]=='M'&&a[len-i-1]=='M')
            sum++;
        else if(a[i]=='O'&&a[len-i-1]=='O')
            sum++;
        else if(a[i]=='T'&&a[len-i-1]=='T')
            sum++;
        else if(a[i]=='U'&&a[len-i-1]=='U')
            sum++;
        else if(a[i]=='V'&&a[len-i-1]=='V')
            sum++;
        else if(a[i]=='W'&&a[len-i-1]=='W')
            sum++;
        else if(a[i]=='X'&&a[len-i-1]=='X')
            sum++;
        else if(a[i]=='Y'&&a[len-i-1]=='Y')
            sum++;
    }
    if(sum==(len/2+1))
        cout<<"TAK"<<endl;
    else
        cout<<"NIE"<<endl;
    return 0;
}

學長給我發的,說我寫的太麻煩了,沒必要那麼多if…

#include<bits/stdc++.h>

using namespace std;
string a = "AbdHIMOopqTUVvWwXxY";
string b = "AdbHIMOoqpTUVvWwXxY";

bool check(char A, char B) {
    for (int i = 0; i < a.size(); i++) {
        if (A == a[i] && B == b[i])return true;
    }
    return false;
}

string s;

int main() {
    cin >> s;
    for (int i = 0; i < s.size(); i++) {
        if (!check(s[i], s[s.size() - i - 1]))return puts("NIE");
    }
    return puts("TAK");
}
發佈了58 篇原創文章 · 獲贊 9 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章