hdu4119-Isabella's Message

这个题是我水出来的,水了一天才水出来的,代码有点乱。

思路:

1,读矩阵,

2,从不同方向四种翻转,每种翻转翻三个90°

3,读入m个单词

4,找出不合要求的情况做好标记

5,输出字典序最小的   明文 

代码如下:(由于变量很多,命名命的我头都大了,所以有点乱)

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
using namespace std;
const int N = 55;
char a[N][N], b[N][N];
string word[4][105];
string s[4], s2[105];
void trans (int n)
{
    char c[N][N];
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            c[i][j] = b[i][j];
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            b[j][i] = c[n-1-i][j];
}
void mu (int k)
{
    string temp = s[k];
    s[k] = "";
    for(int i = 0, flag = 0; i < (int)temp.size(); i++)
    {
        if(temp[i] <= 'z'&& temp[i] >= 'a')
        {
            s[k]+=temp[i];
            flag = 1;
        }
        else if(flag)
        {
            s[k]+=' ';
            flag = 0;
        }
    }
    int count = 0;
    for( int j = s[k].size()-1; j >=0 ; j--)
        if(s[k][j] == ' ')count++;
        else break;
    if(count)
    {
        string wo;
        for(int h = 0; h < (int)s[k].size()-count; h++ )
            wo+=s[k][h];
        s[k] = wo;
    }
}
int com (const void *a, const void *b)
{
    string *c = (string *)a;
    string *d = (string *)b;
    return strcmp(c->data(),d->data());
}
int main ()
{
    //freopen("data.in","r",stdin);
    //freopen("data.out","w",stdout);
    int t, m, n;
    cin>>t;
    int T = 0;
    while(t--)
    {
        T++;
        cout<<"Case #"<<T<<":";
        s[0] = s[1] = s[2] = s[3] = "";
        cin>>n;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                cin>>a[i][j];
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                cin>>b[i][j];
        for(int k = 0; k < 4; k++)
        {
            for(int num = 0; num < 4; num++)
            {
                for(int i = 0; i < n; i++)
                    for(int j = 0; j < n; j++)
                        if(b[i][j] == '*')s[k]+=a[i][j];
                if(num<3)trans(n);
            }
        }
        for(int i = 0; i < 4; i++)
            mu(i);
        cin>>m;
        for(int i = 0; i < m; i++)
            cin>>s2[i];
        for(int i = 0; i < 4; i++)
        {
            string temp;
            for(int k = 0; k < (int)s[i].size(); k++)
            {
                if(s[i][k]!=' ') temp+=s[i][k];
                if(s[i][k] == ' '||k == (int)s[i].size()-1)
                {
                    //cout<<temp<<endl;
                    int v;
                    for(v = 0; v < m; v++)
                        if(temp == s2[v])
                        {
                            v = -100;
                            break;
                        }
                    if(v>0)
                    {
                        s[i][0] = 127;
                        break;
                    }
                    temp = "";
                }
            }
        }
        qsort(s,4,sizeof(string),com);
        if(s[0][0] == 127)
            cout<<" FAIL TO DECRYPT"<<endl;
        else
            cout<<" "<<s[0]<<endl;

    }
    return 0;
}



posted on 2012-08-21 13:43  Primo...  阅读(141)  评论(0)    收藏  举报