【模板】最小表示法

参考题目:洛谷P1368


解析:

联赛完后统一更所有模板题题解


代码:

#include
using namespace std;
#define ll long long
#define re register
#define gc getchar
#define pc putchar
#define cs const

inline
int getint(){
	re int num;
	re char c;
	re bool f=0;
	while(!isdigit(c=gc()))if(c=='-')f=1;num=c^48;
	while(isdigit(c=gc()))num=(num<<1)+(num<<3)+(c^48);
	return f?-num:num;
}

inline
void outint(int a){
	static char ch[13];
	if(a==0)pc('0');
	if(a<0)pc('-'),a=-a;
	while(a)ch[++ch[0]]=a-a/10*10,a/=10;
	while(ch[0])pc(ch[ch[0]--]^48);
}

cs int N=300005;
int a[N<<1];
int n;

signed main(){
	n=getint();
	for(int re i=1;i<=n;++i)a[i]=a[i+n]=getint();
	int p1=1,p2=2;
	while(p1<=n&&p2<=n){
		re int len=0;
		for(;len<n;++len)if(a[p1+len]!=a[p2+len])break;
		if(a[p1+len]>a[p2+len]){
			p1=p1+len+1;
			if(p1==p2)++p1;
		}
		else {
			p2=p2+len+1;
			if(p1==p2)++p2;
		}
	}
	if(p2<=n)swap(p1,p2);
	for(int re i=0;i<n;++i)
	outint(a[p1+i]),pc(' ');
	return 0;
}

你可能感兴趣的:(_____字符串_____)