Uva1584 字符串
逐个比较每一个字符位置开始的字符串。不断更新目前为止,字典序最小串在输入串中的起始位置,最终找到字典序最小串
#define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<string.h> #include<stdio.h> #include<algorithm> #include<memory> #include<stdlib.h> using namespace std; char s[104]; int len; int less1(char *s, int p, int q) { for (int i = 0; i < len; i++) { if (s[(p + i) % len] != s[(q + i) % len]) return s[(p + i) % len] < s[(q + i) % len]; } return 0; } int main() { int t; scanf("%d", &t); while (t--) { int ans = 0; scanf("%s", s); len = strlen(s); for (int i = 0; i < len; i++) { if (less1(s, i, ans)) { ans = i; } } for (int i = 0; i < len; i++) { putchar(s[(ans + i) % len]); } printf("\n"); } return 0; }

浙公网安备 33010602011771号