UVa 120 Stacks of Flapjacks

#include<iostream>
#include<vector>
#include<queue>
#include<numeric>
#include<algorithm>
#include<sstream>
#include<cstdio>
#include<cstring>
#include<string>
#include<ctime>
using namespace std;
int pan[40],P[40];  //P是正确的序列 
int ans[40],k;
void strrev1(int* p)  //UVa 竟然不能用strrev这个 
{
	int* q=k+pan-1;
	while(p<q)
	swap(*p,*q),p++,q--;
	
}
void print(int *A,int n)
{
	for(int i=0;i<=n;i++)
	{
		if(i) putchar(' ');
		cout<<A[i];
	}
	putchar('\n');
}
void solve()
{
	int idx=0;
	memset(ans,0,sizeof(ans));
	memcpy(P,pan,sizeof(int)*k);
	strrev1(pan);
	int n=k;
	sort(P,P+n,greater<int>());
	for(int i=0;i<n;i++)
	{
		if(pan[i]!=P[i])
		{
			if(pan[n-1]!=P[i])
			{
				int* p=find(pan,pan+n,P[i]);
				ans[idx++]=(p-pan)+1;
				strrev1(p);
			}
			strrev1(pan+i);
			ans[idx++]=i+1;
		}
	}
	print(ans,idx);
}
int read()
{
	memset(pan,0,sizeof(int)*k);
	string s;
	getline(cin,s);
	stringstream ss(s);
	k=0;
	while(ss>>pan[k]) k++;  //	while(ss>>pan[k++]) //this is wrong; 
	if(pan[0]=='\0') return 0;  //EOF 
	return 1;
}
int main()
{
	while(read()) print(pan,k-1),solve();
	return 0;
}

你可能感兴趣的:(UVa 120 Stacks of Flapjacks)