Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
这道题我用置换群的算法求最小循环因子,o(n)的复杂度,为什么还要超时,有大量的io吗?#include<iostream> #include<fstream> using namespace std; int convert(int N,int* table){ bool used[200]={0}; int total=1; int i,j,n; for(i=0;i<N;i++){ if(used[i]==0){ n=0; j=i; do{ j=table[j]-1; used[j]=1; n++; }while(j!=i); //cout<<"n"<<endl; total*=n; //cout<<"total"<<total<<endl; } } return total; } int main(){ ifstream cin("tmp.txt"); int N; while(cin>>N&&N!=0){ int* table=new int[N]; int i; for(i=0;i<N;i++){ cin>>table[i]; } int k; int kk=convert(N,table); //cout<<kk<<endl; while(cin>>k&&k!=0){ k%=kk; cin.get(); char* p=new char[N+1]; char* p_convert=new char[N+1]; cin.getline(p,N+1,'\n'); for(i=strlen(p);i<N+1;i++){ p[i]=' '; } p[i]=0; while(k--){ strcpy(p_convert,p); for(i=0;i<N;i++){ p[table[i]-1]=p_convert[i]; } } cout<<p<<endl; delete []p; delete []p_convert; } delete table; } system("pause"); return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator