CDOJ 1270 Playfair

模拟题,代码写得比较乱。。。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;

char s[500000];
char tmp[500000];
char u[500000];
char Map[30][30];
bool flag[500];

vector<int> ans;
int tot;

void f()
{
    tot = 0;
    for (int i = 0; s[i]; i++){
        if (s[i] >= 'a'&&s[i] <= 'z')
            tmp[tot++] = s[i];
        else if (s[i] >= 'A'&&s[i] <= 'Z')
            tmp[tot++] = s[i] - 'A' + 'a';
    }

    tmp[tot] = 0;
    for (int i = 0; tmp[i]; i++)
    if (tmp[i] == 'j') tmp[i] = 'i';
}

void EYET()
{
    for (int i = 0; s[i]; i++){
        if (s[i] >= 'a'&&s[i] <= 'z')
            tmp[tot++] = s[i];
        else if (s[i] >= 'A'&&s[i] <= 'Z')
            tmp[tot++] = s[i] - 'A' + 'a';
    }

    tmp[tot] = 0;
    for (int i = 0; tmp[i]; i++)
    if (tmp[i] == 'j') tmp[i] = 'i';
}

void Fill()
{
    tot = 0;
    memset(Map, 0, sizeof Map);
    memset(flag, 0, sizeof flag);
    for (int i = 0; tmp[i]; i++)
    {
    //    printf("%c %d 
", tmp[i], flag[tmp[i] - 'a']);
        if (flag[tmp[i] - 'a'] == 0)
        {
            flag[tmp[i] - 'a'] = 1;

            tot++;
            int r, c;
            if (tot % 5 == 0) r = tot / 5;
            else  r = tot / 5 + 1;
            c = tot % 5;
            if (c == 0) c = 5;
            Map[r][c] = tmp[i];
        }
    }

    for (int i = 0; i<26; i++)
    {
        char sign = i + 'a';
        if (sign == 'j') continue;
        if (flag[i] == 1) continue;

        tot++;
        int r, c;
        if (tot % 5 == 0) r = tot / 5;
        else  r = tot / 5 + 1;
        c = tot % 5;
        if (c == 0) c = 5;
        Map[r][c] = sign;
    }
}

bool P1(char a, char b)
{
    
    int posxA, posyA; int posxB, posyB;
    for (int i = 1; i <= 5; i++)
    {
        for (int j = 1; j <= 5; j++)
        {
            if (Map[i][j] == a) posxA = i, posyA = j;
            if (Map[i][j] == b) posxB = i, posyB = j;
        }
    }

    if (posxA == posxB)
    {
        posyA++;
        if (posyA > 5) posyA = 1;

        posyB++;
        if (posyB > 5) posyB = 1;

        ans.push_back(Map[posxA][posyA] - 'a');
        ans.push_back(Map[posxB][posyB] - 'a');

        return 1;
    }
    return 0;
}

bool P2(char a, char b)
{

    int posxA, posyA; int posxB, posyB;
    for (int i = 1; i <= 5; i++)
    {
        for (int j = 1; j <= 5; j++)
        {
            if (Map[i][j] == a) posxA = i, posyA = j;
            if (Map[i][j] == b) posxB = i, posyB = j;
        }
    }

    if (posyA == posyB)
    {
        posxA++;
        if (posxA > 5) posxA = 1;

        posxB++;
        if (posxB > 5) posxB = 1;

        ans.push_back(Map[posxA][posyA] - 'a');
        ans.push_back(Map[posxB][posyB] - 'a');

        return 1;
    }
    return 0;
}

void P3(char a, char b)
{
    int posxA, posyA; int posxB, posyB;

    for (int i = 1; i <= 5; i++)
    {
        for (int j = 1; j <= 5; j++)
        {
            if (Map[i][j] == a) { posxA = i, posyA = j; }
            if (Map[i][j] == b) { posxB = i, posyB = j; }
        }
    }

    ans.push_back(Map[posxA][posyB] - 'a');
    ans.push_back(Map[posxB][posyA] - 'a');
}

void work()
{
    int len = strlen(tmp);
    strcpy(u, tmp);
    
    queue<int>Q;
    for (int i = 0; i < len; i++) Q.push(tmp[i]-'a');

    while (!Q.empty())
    {
        char sign1 = Q.front() + 'a';
        char sign2;
        Q.pop();
        if (Q.empty()) break;
        if (Q.front()+'a' == sign1)
        {
            sign2 = 'x';
            if (P1(sign1, sign2) == 1) continue;
            else if (P2(sign1, sign2) == 1) continue;
            else P3(sign1, sign2);
        }
        else
        {
            sign2  = Q.front() + 'a';
            Q.pop();
            if (P1(sign1, sign2) == 1) continue;
            else if (P2(sign1, sign2) == 1) continue;
            else P3(sign1, sign2);
        }
    }
}

int main()
{
    while (gets(s))
    {
        ans.clear();
        f();
        Fill();
        
        bool Exit = 0; tot = 0;
        while (1)
        {
            scanf("%s", s);
            int len = strlen(s);
            if (s[0] == '*' || s[len - 1] == '*') Exit = 1;
            EYET();
            if (Exit) break;
        }tmp[tot] = 0;
        work();
        for (int i = 0; i < ans.size(); i++) printf("%c", ans[i] + 'a');
        printf("
");getchar();
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zufezzt/p/5178643.html