poj 1026 Cipher

这是一道置换题目,我们只要找出每个置换循环的循环节就可以了,对K进行取模,在进行模拟就OK了;

代码1:

View Code
#include<iostream>

  #include<cstdio>

  #include<cstdlib>

  #include<algorithm>

  #include<cmath>

  #include<queue>

  #include<set>

  #include<map>

  #include<cstring>

  #include<vector>

  #include<string>

  #define LL long long

  using namespace std;

  class Node

  {

  public:

        int num[224];

        int cnt;    

  }cir[224];

  class Number

  {

  public:

        int place,num;    

  }number[224];

  int Gcd( int a, int b )

  {

      return b == 0 ? a : Gcd( b , a % b );    

  }

  int Get_circle( int n )

  {

      bool visit[224] = {0};

      int cnt = 0;

      for( int i = 1 ; i <= n ; i ++ )

      {

           if( !visit[i] )

           {    

               int t = number[i].num;

               cir[cnt].cnt = 0;            

               visit[i] = true;

                while( t != i )

                {

                 visit[t] = true;

                 cir[cnt].num[cir[cnt].cnt++] = t;

                 t = number[t].num;    //puts( "asf" );    

               } 

               cir[cnt].num[cir[cnt].cnt++] = i;

               cnt++;        

           }    

      }    

      return cnt;

  }

  int main(  )

  {

      int n,k;

      char str[224],c[224];

      while( scanf( "%d",&n ),n )

      {

           for( int i =1 ; i <= n ; i ++ )

           {

                scanf( "%d",&number[i].num );    

                number[i].place = i;    

           }

           int cnt = Get_circle( n );

           while( scanf( "%d",&k ),k )    

           {

               memset( str , 0 , sizeof( str ) );

               gets( str );

               int L = strlen( str );

              for( int i = L ; i <= n ; i ++ )

                   str[i] = ' ';

               str[n+1] = '\0';

               for( int i = 0 ; i < cnt ; i ++ )

               {

                    int t = k % cir[i].cnt;

                    for( int j = 0; j < cir[i].cnt ; j ++ )

                    {

                         int place = (j+ t)%cir[i].cnt;

                         c[cir[i].num[place]] = str[cir[i].num[j]];

                    }    

               }

               c[n+1] = '\0';

               puts( c + 1 );

           }

           puts( "" );

      }   

      //system( "pause" );

      return 0;

  }

 

 

 

代码2:

View Code
#include<iostream>

 #include<cstdio>

 #include<cstdlib>

 #include<algorithm>

 #include<cmath>

 #include<queue>

 #include<set>

 #include<map>

 #include<cstring>

 #include<vector>

 #include<string>

 #define LL long long

 using namespace std;

 

 void Get_circle( int n,int num[] ,int len[] )

 {

     int cnt = 0;

     for( int i = 1 ; i <= n ; i ++ )

     {

          cnt = 1;

          int t = num[i];

           while( t != i )

           {

             cnt ++;

            t = num[t];        

          } 

          len[i] = cnt;

     }    

 }

 int main(  )

 {

     int n,k,num[224],len[224];

     char str[224],c[224];

     while( scanf( "%d",&n ),n )

     {

          for( int i =1 ; i <= n ; i ++ )

          {

               scanf( "%d",&num[i] );    

          }

          Get_circle( n ,num ,len );

          while( scanf( "%d",&k ),k )    

          {

              memset( str , 0 , sizeof( str ) );

              gets( str );

              int L = strlen( str );

              for( int i = L ; i <= n ; i ++ )

                   str[i] = ' ';

               str[n+1] = '\0';

              int sum = 0;

              for( int i = 1 ; i <= n ; i ++ )

              {                 

                   int t = k % len[i],tt=i;

                   while( t-- )

                   {

                       tt = num[tt]; 

                   }

                  c[tt] = str[i]; 

              }

              c[n+1] = '\0';

              puts( c + 1 );

          }

          puts( "" );

     }   

     //system( "pause" );

     return 0;

 }

 

你可能感兴趣的:(poj)