题目连接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=37
解题思路:
分别模拟四次操作,用数据记录每个数据当前组号,还有就是注意当两个操作对象在同一个组时直接跳过该操作。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
typedef vector<int> vi;
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
const int N = 1e7+5;
void rev(int tt);
int oper1();
int oper2();
int oper3();
int oper4();
void print();
int n;
vi a[100];
vi::iterator it;
int ind[100];
string st,ty;
int fr,to;
int main()
{
cin >> n;
for(int i=0;i<n;i++)
{
a[i].push_back(i);
ind[i] = i;
}
while(cin >> st)
{
if(st=="quit")
{
print();
break;
}
cin >> fr >>ty>>to;
if(fr==to) continue;
if(st=="move")
{
if(ty=="onto")
{
oper1();
}
else
{
oper2();
}
}
else
{
if(ty=="onto")
{
oper3();
}
else
{
oper4();
}
}
/*print();
for(int i=0;i<n;i++)
{
cout << ind[i]<<" ";
}
cout << endl;
cout <<"&&&&&&&&&&&"<<endl;*/
}
return 0;
}
void print()
{
for(int i=0;i<n;i++)
{
cout << i<<":";
for(int j=0;j<a[i].size();j++)
{
cout << " "<< a[i][j];
}
cout <<endl;
}
}
void rev(int tt)
{
ind[tt] = tt;
a[tt].push_back(tt);
for(int i = a[tt].size()-2;i>=0;i--)
{
a[tt][i+1]=a[tt][i] ;
}
a[tt][0] = tt;
}
int oper1(){
int ff =0;
int y = ind[to];
int x = ind[fr];
if(x==y)
return 0;
y = ind[to];
while(*(a[y].end()-1)!=to)
{
int tt = *(a[y].end()-1);
a[y].erase(a[y].end()-1);
rev(tt);
}
x = ind[fr];
while(*(a[x].end()-1)!=fr)
{
int tt = *(a[x].end()-1);
a[x].erase(a[x].end()-1);
rev(tt);
}
y = ind[to];
while(*(a[y].end()-1)!=to)
{
int tt = *(a[y].end()-1);
a[y].erase(a[y].end()-1);
rev(tt);
}
x = ind[fr];
while(*(a[x].end()-1)!=fr)
{
int tt = *(a[x].end()-1);
a[x].erase(a[x].end()-1);
rev(tt);
}
x = ind[fr];
y = ind[to];
a[x].erase(a[x].end()-1);
a[y].push_back(fr);
ind[fr] = y;
}
int oper2(){
int ff =0;
int x = ind[fr];
int y = ind[to];
if(x==y)
return 0;
while(*(a[x].end()-1)!=fr)
{
int tt = *(a[x].end()-1);
a[x].erase(a[x].end()-1);
rev(tt);
}
x = ind[fr];
y = ind[to];
a[x].erase(a[x].end()-1);
a[y].push_back(fr);
ind[fr] = y;
}
int oper3()
{
int ff =0;
int x = ind[fr];
int y = ind[to];
if(x==y)
return 0;
while(*(a[y].end()-1)!=to)
{
int tt = *(a[y].end()-1);
a[y].erase(a[y].end()-1);
rev(tt);
}
stack<int> ss;
while(*(a[x].end()-1)!=fr)
{
int tt = *(a[x].end()-1);
a[x].erase(a[x].end()-1);
ss.push(tt);
}
a[x].erase(a[x].end()-1);
ss.push(fr);
while(!ss.empty())
{
int tt = ss.top();
ss.pop();
a[y].push_back(tt);
ind[tt] = y;
}
}
int oper4(){
int ff =0;
int x = ind[fr];
int y = ind[to];
if(x==y)
return 0;
stack<int> ss;
while(*(a[x].end()-1)!=fr)
{
int tt = *(a[x].end()-1);
a[x].erase(a[x].end()-1);
ss.push(tt);
}
a[x].erase(a[x].end()-1);
ss.push(fr);
while(!ss.empty())
{
int tt = ss.top();
ss.pop();
a[y].push_back(tt);
ind[tt] = y;
}
}