给一个字符串, 将它想象成一个环, 然后从环中任意一个位置断开, 求断开后字典序最小的那种情况。
直接上模板..
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
int a[], n;
int getMin() {
int i, j, k;
for(i = , j = ; i<n&&j<n; ) {
for(k = ; k<n&&a[i+k]==a[j+k]; k++)
;
if(k>=n)
break;
if(a[i+k]<a[j+k])
j += k+;
else
i += k+;
if(i == j)
j++;
}
return i;
}
int main()
{
string s;
while(cin>>s) {
n = s.size();
for(int i = ; i<n; i++) {
a[i] = s[i]-'';
}
int tmp = a[];
for(int i = ; i<n-; i++) {
a[i] = (a[i+]-a[i]+)%;
}
a[n-] = (tmp-a[n-]+)%;
for(int i = n; i<*n; i++)
a[i] = a[i-n];
int pos = getMin();
for(int i = pos; i<pos+n; i++)
printf("%d", a[i]);
cout<<endl;
}
return ;
}