Uva--401 (字符串处理)

 

From 小白书P88

题意:给出若干字符串,判回文和对称(对称的判定以表的形式给出),题目简单,要注意的是输出格式和对称表的建立(这里建了个ASCII码表)

#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;

bool judge_mirro(char *p,int len,int form[128][128]){
    int i;
    for(i = 0; i <= len / 2; i++){
        if(form[p[i]][p[len - 1 - i]] == 0)
            return 0;
    }
    return 1;
}

bool judge_palin(char *p,int len){
    int i;
    for(i = 0; i <= len / 2; i++){
        if(p[i] != p[len - 1 - i])
            return 0;
    }
    return 1;
}

int main(){
    char str[21];
    int f[128][128];
    //init
    memset(f,0,sizeof(f));
    f['A']['A'] = 1;

    f['E']['3'] = 1;
    f['3']['E'] = 1;

    f['H']['H'] = 1;

    f['I']['I'] = 1;

    f['J']['L'] = 1;
    f['L']['J'] = 1;

    f['M']['M'] = 1;
    
    f['O']['O'] = 1;

    f['S']['2'] = 1;
    f['2']['S'] = 1;

    f['T']['T'] = 1;

    f['U']['U'] = 1;

    f['V']['V'] = 1;

    f['W']['W'] = 1;

    f['X']['X'] = 1;

    f['Y']['Y'] = 1;

    f['Z']['5'] = 1;
    f['5']['Z'] = 1;

    f['1']['1'] = 1;

    f['8']['8'] = 1;

    int flag1,flag2,l;
    while(scanf("%s",str) != EOF){
        l = strlen(str);
        flag1 = judge_mirro(str,l,f);
        flag2 = judge_palin(str,l);
        if(flag1 && flag2)
            printf("%s -- is a mirrored palindrome.\n",str);
        else if(flag1 && !flag2)
            printf("%s -- is a mirrored string.\n",str);
        else if(!flag1 && flag2)
            printf("%s -- is a regular palindrome.\n",str);
        else
            printf("%s -- is not a palindrome.\n",str);
        puts("");
    }
    return 0;
}
 

 

posted @ 2014-05-28 14:49  Naturain  阅读(117)  评论(0编辑  收藏  举报