zoukankan      html  css  js  c++  java
  • uva 101

    /*
      
    	(栈)这里 有一个需要知道的  如果一个点走出了 原来的 位置 那么它最早的那个位置的  当他在回到原来位置的时候 可能还会有 块在那个下面 
      根据这点 我们就可以 大胆 的进行出栈入栈操作 
    */
    
    
    #include <string.h>
    #include <iostream>
    #include <cstdio>
    using namespace std;
    int L[30],N[30][30],num[30],t1,t2,A,B,KH[30],n;
    void dec(int er){
        int i,LOC=L[er];
    	for(i=num[LOC];i>=1;i--){   if(N[LOC][i]==er) break; int d=N[LOC][i];  num[LOC]--;N[d][++num[d]]=d; L[d]=d;}
    }
    void pustt(int er,int to){
       int i,LOC=L[er];
       int LL=0;
       for(i=num[LOC];i>=1;i--){ 
    	   int d=N[LOC][i]; 
                KH[++LL]=d;  
    			L[d]=L[to];  
    			num[LOC]-- ;  
    			if(N[LOC][i]==er) break;  
       }
       LOC=L[to];
       while(LL!=0){
    	   N[LOC][++num[LOC]]= KH[LL];
    	   LL--; 
       }
    }
    void print(){
    	for(int i=0;i<n;i++){
    	   printf("%d:",i);
    	   for(int j=1;j<=num[i];j++) printf(" %d",N[i][j]);
    	   printf("
    ");
    	}
      
    }
    int main()
    {
    	int i;
    	scanf("%d",&n);
    	char str[100];
        memset(num,0,sizeof(num));
    	memset(N,0,sizeof(N));
    	memset(L,0,sizeof(L));
    	for(i=0;i<n;i++){
    	   L[i]=i;
    	   N[i][++num[i]]=i;
    	}
    	while(scanf("%s",str)==1){
    	  if(str[0]=='q')break;
    
    	  if(str[0]=='m')t1=1;
    	  else t1=2; 
    	  scanf("%d%s%d",&A,str,&B);
           if(str[1]=='n')t2=1;
    	   else t2=2;
    	   if(A>=n||A<0||B>n||B<0||L[A]==L[B])continue;
    	   if(t1==1&&t2==1){ dec(A);
    	   dec(B); 
    	   N[L[B]][++num[L[B]]]=A; num[L[A]]--;
    	   L[A]=L[B]; 
    	   }
    	   if(t1==1&&t2==2){  dec(A);    N[L[B]][++num[L[B]]]=A; num[L[A]]--; L[A]=L[B];   }
    	   if(t1==2&&t2==1){   dec(B);pustt(A,B);     }
    	   if(t1==2&&t2==2){   pustt(A,B);          }
    	
    	}
           print();
       return 0;
    
    }

  • 相关阅读:
    Leetcode 811. Subdomain Visit Count
    Leetcode 70. Climbing Stairs
    Leetcode 509. Fibonacci Number
    Leetcode 771. Jewels and Stones
    Leetcode 217. Contains Duplicate
    MYSQL安装第三步报错
    .net 开发WEB程序
    JDK版本问题
    打开ECLIPSE 报failed to load the jni shared library
    ANSI_NULLS SQL语句
  • 原文地址:https://www.cnblogs.com/Opaser/p/3662024.html
Copyright © 2011-2022 走看看