UVA_401:Palindromes

 AC:Time(29ms)     C++ 4.8.2

#include<stdio.h>
#include<string.h>
char * mirstr = "AAE3EHHIIJLJMM0OS2STTUUVVWWXXYYZ5Z88";
int dispose(char *);
int main(void)
{
    char str[25];
    int state;
    while(scanf("%s", str) == 1)
    {
        state = dispose(str);
        switch(state)
        {
            case 1: printf("%s -- is not a palindrome.\n\n", str); break;
            case 2: printf("%s -- is a regular palindrome.\n\n", str); break;
            case 3: printf("%s -- is a mirrored string.\n\n", str); break;
            case 4: printf("%s -- is a mirrored palindrome.\n\n", str); break;
        }
    }

    return 0;
}

int dispose(char * str)
{
    int i;
    bool palin = true;
    bool mirr = true;
    int len = strlen(str);
    int half_len = len / 2;
    // 判断回文
    for(i = 0; i < half_len; i++)
        if(str[i] != str[len-1-i])
            palin = false;

    char * p;
    // 判断镜像
    for(i = 0; i < len; i++)
    {
        p = strchr(mirstr, str[i]);
        if(!p)
        {
            mirr = false;
            break;
        }

        if(p[0] == ‘O‘ || p[0] == ‘0‘) // 字母O和数字0单独处理
        {    // 一开始这个花括号忘记了,导致最后一个测试数据失败,情况四变成了情况二,通过调试检查出了错误。
            if(str[len-1-i] != ‘O‘ && str[len-1-i] != ‘0‘)
            {
                mirr = false;
                break;
            }
        }
        else if(p[1] != str[len-1-i])
        {
            mirr = false;
            break;
        }
    }    

    if(palin == false && mirr == false)
        return 1;
    else if(palin == true && mirr == false)
        return 2;
    else if(palin == false && mirr == true)
        return 3;
    else
        return 4;

}
时间: 2024-10-02 02:45:31

UVA_401:Palindromes的相关文章

[UVa] Palindromes(401)

UVA - 401 Palindromes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDED

uva 11584 Partitioning by Palindromes 线性dp

// uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串的数目 // // f[i] = min(f[i],f[j-1] + 1(j到i是回文串)) // // 这道题还是挺简单的,继续练 #include <algorithm> #include <bitset> #include <cassert> #include <

Codeforces Round #316 (Div. 2)E. Pig and Palindromes DP

E. Pig and Palindromes Peppa the Pig was walking and walked into the forest. What a strange coincidence! The forest has the shape of a rectangle, consisting of n rows and m columns. We enumerate the rows of the rectangle from top to bottom with numbe

Codeforces #316 E Pig and Palindromes DP

// Codeforces #316 E Pig and Palindromes // // 题目大意: // // 给你一张地图,n*m每个点是一个字母,现在从(0,0)出发, // 每次只能往右或者往下走,求走到(n-1,m-1)形成回文串的方法数. // // 解题思路: // // 动态规划.首先.如果起点和终点的字母不相同,那么肯定 // 不能形成回文串,直接输出0.对于能形成回文串.我们设状态 // d(step,i,j)表示走了step步,从第0行走到i行,第n-1行走到j行的 /

UVA11584---Partitioning by Palindromes(dp)

We say a sequence of characters is a palindrome if it is the same written forwards and backwards. For exam- ple, ' racecar ' is a palindrome, but ' fastcar ' is not. A partition of a sequence of char- acters is a list of one or more disjoint non-empt

UVa 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 i

UVA 之401 - 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 ri

1167: 零起点学算法74——Palindromes _easy version

1167: 零起点学算法74--Palindromes _easy version Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lldSubmitted: 1754  Accepted: 1023[Submit][Status][Web Board] Description "回文串"是一个正读和反读都一样的字符串,比如"level"或者"noon"等等就是回文串.请写一个

洛谷P1207 [USACO1.2]双重回文数 Dual Palindromes

P1207 [USACO1.2]双重回文数 Dual Palindromes 291通过 462提交 题目提供者该用户不存在 标签USACO 难度普及- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 如果一个数从左往右读和从右往左读都是一样,那么这个数就叫做“回文数”.例如,12321就是一个回文数,而77778就不是.当然,回文数的首和尾都应是非零的,因此0220就不是回文数. 事实上,有一些数(如21),在十进制时不是回文数,但在其它进制(如二进制时为10101)时就是回文数. 编