算法竞赛入门经典字符串推荐题目

题目

Origin Title Satus
UVA 401 Palidromes AC
UVA 10010 Where’s Waldorf? AC
UVA 10361 Automatic Poetry AC
UVA 537 Artificial Intelligence? AC
UVA 409 Excuses, Excuses! AC
UVA 10878 Decode the tape AC
UVA 10815 Andy’s First Dictionary AC
UVA 644 Immediate Decodability AC
UVA 10115 Automatic Editing AC

UVA 401 Palidromes

分析

  1. 镜面字符串应关于中间位置镜面对称,特别地,若中间位置为一字符则该字符也应自镜面对称,所以字符串的字符个数的奇偶需要考量.
  2. 因为数字0和字母O可视为一致,因此仅字母O是合法输入,即不必考虑输入数据包含数字0.
  3. 注意格式:每输出一行数据,应跟随输出一空行.

代码

#include <stdio.h>
#include <string.h>

int is_palindrome(char* str, int len)
{
    int i;
    for (i = 0; i < len/2; i++)
        if (*(str + i) != *(str + (len-1) - i))
            return 0;
    return 2;
}

int is_mirrored(char* str, int len)
{
    const char* ch = "AEHIJLMOSTUVWXYZ12358";
    const char* re = "A3HILJMO2TUVWXY51SEZ8";
    int i, j;
    for (i = 0; i < len/2+1; i++) {
        for (j = 0; j < 21; j++) {
            if (*(str + i) == *(ch + j)) {
                if (*(str + (len-1) - i) != *(re + j))
                    return 0;
                break;
            }
        }
        if (j == 21) return 0;
    }
    return 1;
}

int main(void)
{
    char str[25];
    int len = 0;
    while (scanf("%s", str) != EOF) {
        printf("%s", str);
        len = strlen(str);
        switch (is_palindrome(str, len) | is_mirrored(str, len)) {
        case 0: /* 00 */
            printf(" -- is not a palindrome.");
            break;
        case 2: /* 10 */
            printf(" -- is a regular palindrome.");
            break;
        case 1: /* 01 */
            printf(" -- is a mirrored string.");
            break;
        case 3: /* 11 */
            printf(" -- is a mirrored palindrome.");
            break;
        }
        printf("\n\n");
    }
    return 0;
}

UVA 10010 Where’s Waldorf?

分析

  1. 所需搜寻的模式必定出现且仅出现一次,但大小写不敏感.
  2. 先遍历确定首字母,再进入搜索,因为模式仅沿八个方向中的固定一个,所以可以滚一遍八方向.

代码

#include <stdio.h>
#include <string.h>

int m, n, x, y;
char map[55][55];
int next[8][2] = {
  {
  1,
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值