ZOJ 1009 轉子加密

來源:互聯網
上載者:User

//這裡有新的輸入輸出格式,注意其實輸入格式跟輸出格式是分開的,即系就算某一輸出改變在另一輸入前,
//只要輸出格式符合要求就行
//用三重for做
#include<iostream>
#include<string>
using namespace std;
int st[3][30],sttemp[3][30];
char s[10000];

 

void insert(int n)
{
    int i,j;
    char s;
    //先輸入轉子
    for(i=0;i<3;i++)
        for(j=0;j<n;j++)
        {
            cin>>s;
            sttemp[i][j]=s-'A';
        }

    //把編碼轉子轉為解碼轉子
    for(i=0;i<3;i++)
        for(j=0;j<n;j++)
            st[i][sttemp[2-i][j]]=j;    //其實就是數組值與下標的對應轉換

    for(i=0;i<3;i++)
        for(j=0;j<n;j++)
            sttemp[i][j]=st[i][j];

}

//此函數是讓第a個轉子移一下位,同時解碼轉子改變
//轉子移一位其實是st[a][i]的值等於其前一個st[a][i+1]的值+1
void rotor(int a,int n)   
{
    int temp,i;
    //由於有覆蓋,要先儲存st[a][n-1]
    temp=st[a][n-1];
    for(i=n-1;i>=1;i--)
        st[a][i]=( st[a][i-1]+1 )%n;
    st[a][0]=(temp+1)%n;
}

 

int main()
{
    int n,count=1;
    //最外這個迴圈是test迴圈
    cin>>n;
    while(n!=0)   
    {
        cout<<"Enigma "<<count<<":"<<endl;
        count++;
        int crypnum;
        insert(n);
        cin>>crypnum;
        //此迴圈是對同一test的不同資料
        while(crypnum--)
        {
            int i,j,k,temp,len;
            char t;
            bool flags;
            cin>>s;
            len=strlen(s);

            //利用三重for迴圈來方便對轉子何時移位做判斷
            //flags記錄當s[]每一位字元碼都譯好以後,退出迴圈
            flags=1;                 
            for(i=0;i<len;i++)
            {
                if(flags==0)
                    break;
                for(j=0;j<n;j++)
                {
                    if(flags==0)
                        break;
                    for(k=0;k<n;k++)
                    {
                        if(s[i*n*n+j*n+k]=='\0')
                        {
                            flags=0;
                            break;
                        }
                        temp=s[i*n*n+j*n+k]-'A';
                        //輸出st的經過幾個轉子以後的原碼
                        //字元要藉助t才能輸出
                        t=st[2][   st[1][ st[0][temp] ]   ]+'a';
                        cout<<t;    
                        rotor(2,n);        //轉子2移一位
                    }
                    rotor(1,n);        //轉子1移一位
                }
                rotor(0,n);         //轉子0移一位
            }
            cout<<endl;
            //轉子複原
            for(i=0;i<3;i++)
                for(j=0;j<n;j++)
                    st[i][j]=sttemp[i][j];

        }
        
        cin>>n;                       //為了輸出時能在test之間空行
        if(n!=0)
            cout<<endl;

    }
    return 0;
}

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.