HDU - 4119 Isabella's Message

Description

Isabella and Steve are very good friends, and they often write letters to each other. They exchange funny experiences, talk about people around, share their feelings and write about almost everything through the letters. When the letters are delivered, they are quite afraid that some other people(maybe their parents) would peek. So they encrypted the letter, and only they know how to decrypt it. This guarantees their privacy.
The encrypted message is an N * N matrix, and each grid contains a character.
Steve uses a special mask to work as a key. The mask is N * N(where N is an even number) matrix with N*N/4 holes of size 1 * 1 on it.
The decrypt process consist of the following steps:
1. Put the mask on the encrypted message matrix
2. Write down the characters you can see through the holes, from top to down, then from left to right.
3. Rotate the mask by 90 degrees clockwise.
4. Go to step 2, unless you have wrote down all the N*N characters in the message matrix.
5. Erase all the redundant white spaces in the message.
For example, you got a message shown in figure 1, and you have a mask looks like figure 2. The decryption process is shown in figure 3, and finally you may get a message "good morning".

You can assume that the mask is always carefully chosen that each character in the encrypted message will appear exactly once during decryption.
However, in the first step of decryption, there are several ways to put the mask on the message matrix, because the mask can be rotated (but not flipped). So you may get different results such as "od morning go" (as showed in figure 4), and you may also get other messages like "orning good m", "ng good morni".
                                                                                                  
Steve didn‘t know which direction of the mask should be chosen at the beginning, but after he tried all possibilities, he found that the message "good morning" is the only one he wanted because he couldn‘t recognize some words in the other messages. So he will always consider the message he can understand the correct one. Whether he can understand a message depends whether he knows all the words in the message. If there are more than one ways to decrypt the message into an understandable one, he will choose the lexicographically smallest one. The way to compare two messages is to compare the words of two messages one by one, and the first pair of different words in the two messages will determine the lexicographic order of them.
Isabella sends letters to Steve almost every day. As decrypting Isabella‘s message takes a lot of time, and Steve can wait no longer to know the content of the message, he asked you for help. Now you are given the message he received, the mask, and the list of words he already knew, can you write a program to help him decrypt it?

Input

The first line contains an integer T(1 <= T <= 100), indicating the number of test cases.
Each test case contains several lines.
The first line contains an even integer N(2 <= N <= 50), indicating the size of the matrix.
The following N lines each contains exactly N characters, reresenting the message matrix. The message only contains lowercase letters and periods(‘.‘), where periods represent the white spaces.
You can assume the matrix contains at least one letter.
The followingN lines each containsN characters, representing the mask matrix. The asterisk(‘*‘) represents a hole, and period(‘.‘) otherwise. The next line contains an integer M(1 <= M <= 100), the number of words he knew.
Then the following M lines each contains a string represents a word. The words only contain lowercase letters, and its length will not exceed 20.

Output

For each test case in the input, print one line: "Case #X: Y", where X is the test case number (starting with 1) and Y is Isabella‘s message.
If Steve cannot understand the message, just print the Y as "FAIL TO DECRYPT".

题目大意:给你一个n*n的格子,格子里面有字母和空格,然后你拿挡板去截取字母,然后问你是否能够还原。

具体思路:这么小的数据范围,模拟一下就好了,非常简单我才不会说我PE了2发呢

单词有没有出现过可以用map判(记得要清空),答案什么的用string非常方便

要注意的还有答案的最后不能有空格否则会PE

字典序最小的要求只要把可行的答案扔到数组里,sort一下就好了

AC代码

#include<bits/stdc++.h>
using namespace std;
int i,j,n,T,m,t;
char mp[1000][1000],key[1000][1000],key2[1000][1000];
string word[200];
string hhh[10],ans[10],hhhh,tot;
map<string,bool> bo;
bool ok;
void zhuan()
{
    for (int i=1;i<=n;i++)for (j=1;j<=n;j++)key2[i][j]=key[i][j];
    for (int i=1;i<=n;i++)for (j=1;j<=n;j++)key[i][j]=key2[j][n-i+1];
}
int main()
{
    scanf("%d",&T);
    while (T--)
    {
        t++,bo.clear(),scanf("%d",&n);
        for (i=1;i<=n;i++)scanf("%s",mp[i]+1);
        for (i=1;i<=n;i++)scanf("%s",key[i]+1);
        scanf("%d",&m);
        for (i=1;i<=m;i++)cin>>word[i],bo[word[i]]=true;
        for (i=1;i<=4;i++)
        {
            zhuan(),zhuan(),zhuan();//?òo???°?Dy×a·??ò??·′á?,·′?y×aèy′??íoíòa?óò??ùá?
            hhh[i]="";
            for (int x=1;x<=n;x++)
                for (int y=1;y<=n;y++)
                    if(key[x][y]==‘*‘)
                        if(mp[x][y]!=‘.‘)
                        hhh[i]=hhh[i]+mp[x][y];
                        else hhh[i]=hhh[i]+" ";
        }
        for (i=1;i<=4;i++)ans[i]="";
        int anstop=0;
        for (i=1;i<=4;i++)
        {
            tot=hhh[1]+hhh[2]+hhh[3]+hhh[4],ok=true,anstop++,ans[anstop]="";
            for (j=0;j<tot.length();j++)
            {
                string wo="";
                while (tot[j]==‘ ‘&&j<tot.length())j++;
                while (tot[j]!=‘ ‘&&j<tot.length())wo=wo+tot[j],j++;
                if(!bo[wo]&&wo!="")
                {
                    ok=false;
                    break;
                }
                ans[anstop]=ans[anstop]+wo+" ";
            }
            if(!ok)anstop--;
            hhhh=hhh[1],hhh[1]=hhh[2],hhh[2]=hhh[3],hhh[3]=hhh[4],hhh[4]=hhhh;
        }
        printf("Case #%d: ",t);
        if(anstop)
        {
            sort(ans+1,ans+anstop+1);
            string::iterator it=ans[1].end()-1;
            while (*it==‘ ‘)ans[1].erase(it),it=ans[1].end()-1;
            cout<<ans[1]<<endl;
        }else puts("FAIL TO DECRYPT");
    }
    return 0;
}

HDU - 4119 Isabella's Message

时间: 2024-12-20 20:29:04

HDU - 4119 Isabella's Message的相关文章

hdu 4119 Isabella&#39;s Message【字符串模拟】

题目链接:http://write.blog.csdn.net/postedit 自我感觉比较麻烦 #include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #include<string> #include<map> using namespace std; const int maxh=100+10; const int maxe=100

HDU 4119 Isabella&#39;s Message (2011年成都赛区现场赛I题)

1.题目描述:点击打开链接 2.解题思路:本题是一道模拟题,要求模拟一个解密的过程,练习这么久第一次做模拟题1Y了,内心还是很激动的~.只需要根据题意,记录* 所在的位置即可,然后每次都是先解密,后顺时针旋转90度.把每次解密的信息放到一个vector里,接下来就是连接它们,得到解密后的字符串,在map中查找这些单词是否存在即可.如果都存在,就把这条解密信息放到ans中,最后对ans排序,输出ans[0]就是答案. 3.代码: //#pragma comment(linker, "/STACK:

hdu 4119 Isabella&#39;s Message 模拟题

Isabella's Message Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4119 Description Isabella and Steve are very good friends, and they often write letters to each other. They exchange funny experiences, talk ab

hdu 4119 Isabella&#39;s Message 【字符串处理】

Isabella's Message Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2098    Accepted Submission(s): 614 Problem Description Isabella and Steve are very good friends, and they often write letters

hdu 4300 Clairewd’s message (KMP)

给定一个翻译表,即第i个字母用哪个字母表示 再给一个串,里面前面为密文,后面为明文,密文一定是完整的,但明文不完整或可能没有 求这个完整的前面密文后面明文的串 # include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int len; int next[100010]; char a1[100010],a2[100010],a3[100010]; void Ge

HDU 4300 Clairewd&#39;s message ( 拓展KMP )

题意 : 给你一个包含26个小写字母的明文密文转换信息字符串str,第一个表示'a'对应的密文是str[0].'b'对应str[1]--以此类推.接下来一行给你一个另一个字符串,这个字符串由密文+明文组成,但是现在后面部分的明问可能有不完整的情况(也有可能缺失只包含密文),问你现在最少需要补充多多少个字符串才能使得字符串变成完整的满足==>密文+密文对应的明文 组成的字符串,将这个完整的字符串输出出来. 分析 : 冷静分析一下可以发现,在给出的残缺字符串中,前面的一半肯定是属于密文的!如果不是这

HDU 4300 Clairewd‘s message 拓展KMP入门

HDU 4300 Clairewd's message 拓展KMP入门 题意 原题链接 这个题关键是要读懂题意,我做的时候就没有读懂,泪.题意是说给你的一个两个字符串,一个是26个字母密码表,依次对应替换的字母.然后给你一个字符串,这个字符串是不完整的(完整的应该是前半部分是加密的,后半部分是解密了的),然而,给你的字符串一定是加密的部分+一部分解密的部分(可以是全部,也可以是没有),让你求出最短的完整字符串,包括密文和明文: 解题思路 考虑给出的字符串S加密部分一定全部给出,所以给出的字符串的

hdu 4300 Clairewd’s message(详解,扩展KMP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4300 Problem Description Clairewd is a member of FBI. After several years concealing in BUPT, she intercepted some important messages and she was preparing for sending it to ykwd. They had agreed that ea

HDU 4300 Clairewd’s message

Problem Description Clairewd is a member of FBI. After several years concealing in BUPT, she intercepted some important messages and she was preparing for sending it to ykwd. They had agreed that each letter of these messages would be transfered to a