poj 1026 Cipher(置換群T_T)

來源:互聯網
上載者:User

跪了一個下午了,才發現在迴圈哪裡寫錯了WA到死。

題目意思是:

104 5 3 7 2 8 1 6 10 91 Hello Bob1995 CERC00


給出了一個長為N的序列,和需要按這序列交換k次的字串 (如長度不足n,就補空格) .交換方式時字串下標為i與序列中num[i]進行交換。

做法,還是根據迴圈節,既然它都說按照這個關係交換的,那就有個規律進過L就換回原來位置,這個L就是迴圈節長度,交換k次,k個L略去K%L就是其最後的結果。

k沒限制大小所以直接類比必然會逾時.


/*Problem ID:poj 1026Meaning: 將i上的字元與num[i]交換k次Analyzing:置換群組合數學*/#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<vector>using namespace std;#define FOR(i,s,t) for(int i=(s); i<(t); i++)#define LL long long#define BUG puts("here!!!")#define STOP system("pause")#define file_r(x) freopen(x, "r", stdin)#define file_w(x) freopen(x, "w", stdout)#define maxn 205LL gcd(LL a,LL b) {return a?gcd(b%a,a):b;}int main(){    int n,num[maxn],k,L[maxn];    char str[maxn],str1[maxn];    while(scanf("%d",&n)&&n){        FOR(i,0,n) {scanf("%d",&num[i]);num[i]-=1;}        memset(L,0,sizeof(L));        FOR(i,0,n){            int j=num[i];            while(1){                L[i]++;                j=num[j];                if(j==num[i]) break;            }        }        while(scanf("%d",&k)&&k){            getchar();            gets(str);            int len=strlen(str);            FOR(i,len,n) str[i]=' ';            str[n]='\0';            memset(str1,0,sizeof(str1));            strcpy(str1,str);            FOR(i,0,n){                int l=i,r=i;                FOR(j,0,k%L[i])                    r=num[r];                str1[r]=str[l];            }            puts(str1);            }            puts("");        }        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.