想法:

若要回文,則須滿足

 

1.回文序列是奇數

英文字母的數量必須一個是奇數,而且,也只能一個是奇數

 

2.回文序列是偶數

英文字母的數量不能有任何一個是奇數

 

迴圈

輸入字串

將字串中的英文通通變小寫

記錄其數量(此處方法請參照ASCII)

依上面規則判別奇數個數

 

 

輸出

 

#include <iostream>
#include <cstring>
#include <cctype>

using namespace std;

void cnt(string str)
{
    int alphaNum = 0;
    int charNum[26] = {0};
    int odd = 0;

    for(int i = 0; i<str.length(); i++)
        if(isalpha(str[i]))
        {
            alphaNum++;
            if(isupper(str[i]))
                charNum[(int)str[i] - 65]++;
            else
                charNum[(int)str[i] - 97]++;
        }

    for(int i = 0; i<26; i++)
        if(charNum[i]%2)
        {
            odd++;

            if(odd>=2 || !alphaNum%2)
            {
                cout << "no...\n";
                return;
            }
        }

    cout << "yes !\n";
    return;
}

int main()
{
    int charNum[127] = {0};
    string str;

    while(cin >> str)
        cnt(str);

    return 0;
}

arrow
arrow
    全站熱搜

    大神(偽) 發表在 痞客邦 留言(0) 人氣()