POJ-1509 Glass Beads---最小表示法模板

题目链接:

https://vjudge.net/problem/POJ-1509

题目大意:

给你一个循环串,然后找到一个位置,使得从这个位置开始的整个串字典序最小。

解题思路:

最小表示法模板

注意模板返回的下标是从0开始,答案要求从1开始

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<string>
 6 #include<set>
 7 using namespace std;
 8 const int maxn = 100000 + 10;
 9 const int INF = 0x3f3f3f3f;
10 int change_min(char s[])
11 {
12     int n = strlen(s);
13     int i = 0, j = 1, k = 0;
14     while(i < n && j < n && k < n)
15     {
16         int t = s[(i + k) % n] - s[(j + k) % n];
17         if(!t)
18             k++;
19         else
20         {
21             if(t > 0)
22                 i += k + 1;
23             else
24                 j += k + 1;
25             if(i == j)j++;
26             k = 0;
27         }
28     }
29     return i < j ? i : j;
30 }
31 char s[maxn];
32 int main()
33 {
34     int T;
35     scanf("%d", &T);
36     while(T--)
37     {
38         cin >> s;
39         cout<<(change_min(s) + 1)<<endl;
40     }
41     return 0;
42 }

猜你喜欢

转载自www.cnblogs.com/fzl194/p/8934070.html
今日推荐