UVA 1584 字符串

VJ 该题 链接  https://vjudge.net/problem/UVA-1584

AC代码   字典序最小输出

 1 #include <stdio.h>
 2 #include <math.h>
 3 #include <string.h>
 4 #include <stdlib.h>
 5 #include <iostream>
 6 #include <sstream>
 7 #include <algorithm>
 8 #include <string>
 9 #include <queue>
10 #include <vector>
11 using namespace std;
12 const int maxn= 105;
13 const int inf = 0x3f3f3f3f;
14 typedef long long ll;
15 char s[maxn];
16 int compare(char *s,int q,int p)
17 {
18   int n=strlen(s);
19   for(int i=0;i<n;i++)               //按字典序比较
20   {
21     if(s[(q+i)%n]!=s[(p+i)%n])
22       return s[(q+i)%n]<s[(p+i)%n];
23   }
24   return 0;  //相等
25 }
26 int main(int argc, char const *argv[])
27 {
28   int t;
29   cin>>t;
30   while(t--)
31   {
32     scanf("%s",s);
33     int ans=0;
34     int len=strlen(s);
35     for(int i=1;i<len;i++)
36     {
37       if(compare(s,i,ans))
38         ans=i;
39     }
40     for(int i=0;i<len;i++)
41     {
42       printf("%c",s[(ans+i)%len]);
43     }
44     printf("
");
45   }
46   return 0;
47 }
原文地址:https://www.cnblogs.com/stranger-/p/7490188.html