• 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;
    }
  • 相关阅读:
    DataGridView 控件和 DataGrid 控件之间的区别
    winform 如何创建mdi属性IsMdiContainer=true
    窗体分为左右两部分,要求在左边栏点击按钮时,右边动态加载窗体
    OLEDBConnection 和SQLConnection 有什么区别
    成为一个顶级设计师的八大秘诀
    青春追梦
    四维领导力:明道、取势、优术、树人
    走好青春
    再贺开业
    骑车在暴雨中有感
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5178643.html
Copyright © 2020-2023  润新知