紫书——Circular Sequence UVA - 1584

题解:

这题目要求的是求出字典序最小的循环排列,那么枚举出所有开头拼成的字符串,每次比较一次就行

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

int main() {
	//freopen("in.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	
	
	int n;
	scanf("%d",&n);
	for(int i = 0; i < n; i++){
		string str,minstr;
		cin >> str; minstr = str;
		int len = str.size();
		for(int j = 1; j < len; j++){
			string tmp = str.substr(j)+str.substr(0,j);
			if(tmp.compare(minstr) < 0) minstr = tmp;
		}
		cout << minstr << endl;
	}

	return 0;
}

你可能感兴趣的:(紫书)