POJ 1026 Ciper(置換裡的迴圈)

來源:互聯網
上載者:User
題目分析

這道題的輸入處理起來有點麻煩,要用gets讀取一行,注意前面還有一個空格。然後就是找迴圈節,用vector儲存,每個vector的大小就是一個迴圈節的大小,然後處理一下即可,之一每個案例結束後還要加一個分行符號。

#include <cstdio>#include <vector>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 205;int a[maxn], vis[maxn];vector <int> vec[maxn];char s[maxn];int main(){    int n, m;    while(scanf("%d", &n) != EOF && n){        for(int i = 1; i <= n; i++) scanf("%d", &a[i]);        memset(vis, 0, sizeof(vis));        for(int i = 0; i < maxn; i++) vec[i].clear();        int tot = 0;        for(int i = 1; i <= n; i++) if(!vis[i]){            int temp = i;            while(!vis[temp]){                vis[temp] = 1;                vec[tot].push_back(temp);                temp = a[temp];            }            tot++;        }        while(scanf("%d", &m) == 1 && m){  //這裡面m表示置換的次數            getchar();            gets(s+1);            int len = strlen(s+1);            for(int i = len+1; i <= n; i++) s[i] = ' ';  //注意在後面加空格            s[n+1] = '\0'; //不要忘了字串結束符            char ss[maxn];            for(int i = 0; i < tot; i++){   //這裡進行轉換                int len = vec[i].size();                for(int j = 0; j < len; j++) ss[vec[i][(j+m)%len]] = s[vec[i][j]];            }            ss[n+1] = '\0';            printf("%s\n", ss+1);        }        printf("\n");    }    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.