UVA11221 Magic square palindromes.【Ad Hoc】

A magic square palindrome is a sentence whose characters can be divided in a K ×K square table with the property that the original sentence can be read from the table in four different ways:
• Start from the (1,1) cell, move right until the end of the line and than proceed to the next line.
• Start from the (1,1) cell, move down until the end of the column and then proceed to the next column.
• Start from the (K, K) cell, move left until the beginning of the line and then proceed to the previous line.
• Start from the (K, K) cell, move up until the beginning of the column and then proceed to the previous column.
    “Sator arepo tenet opera rotas” is probably the most famous magic square palindrome. We can arrange it in a K = 5 (5 × 5) table in the following way:
s a t o r
a r e p o
t e n e t
o p e r a
r o t a s
在这里插入图片描述
    Notice that the original sentence can be read from the table in the four different ways described above.
Input
The first line of input gives the number of cases, T (1 ≤ T ≤ 60). T test cases follow. Each one contains one line L, such that 0 < length(L) < 10.000, with the sentence (that can contain characters [a-z], whitespace, comma, period, interrogation and exclamation points and parentheses). Please notice that the magic square palindrome table ignores all whitespace and punctuation symbols.
Output
The output is comprised of two lines for each input data set. The first line identifies the data set with a number (starting from one and incrementing at each new data set). The second line gives the dimension of the magic square palindrome table (the K value described above) or the expression ‘No magic ?’ if the sentence is not a magic square palindrome.
Sample Input
3
sator arepo tenet opera rotas
this sentence is, quite clearly, not a magic square palindrome! but then again, you never know…
muse sun, eve.s e(y)es even use sum.
Sample Output
Case #1:
5
Case #2:
No magic ?
Case #3:
5

问题链接UVA11221 Magic square palindromes.
问题简述:(略)
问题分析
    简单题,有关回文的问题,给代码不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA11221 Magic square palindromes. */

#include <bits/stdc++.h>

using namespace std;

const int N = 10000, K = 100;
char s[N + 1], mp[K][K];

int main()
{
    int t, caseno = 0;
    scanf("%d", &t);
    getchar();
    while(t--) {
        gets(s);

        int k = 0;
        for(int i = 0; s[i]; i++)
            if(islower(s[i]))
                s[k++] = s[i];
        s[k] = '\0';

        int n = 0;
        for(int i = 1; i <= K; i++)
            if(i * i == k) {
                n = i;
                break;
            }

        if(n) {
            for(int i = 0; i < n; i++)
                for(int j = 0; j < n; j++)
                    mp[i][j] = s[i * n + j];

            for(int i = 0; i < n; i++)
                for(int j = 0; j < n; j++)
                    if(mp[i][j] != mp[j][i])
                        n = 0;
                    else if(mp[i][j] != mp[n - 1 - i][n - 1 - j])
                        n = 0;
                    else if(mp[j][i] != mp[n - 1 - i][n - 1 - j])
                        n = 0;
        }

        printf("Case #%d:\n", ++caseno);
        if (n) printf("%d\n", n);
        else printf("No magic :(\n");
    }

    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值