POJ-1509 Glass Beads---Minimal Notation Template

Topic link:

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

Topic meaning:

You are given a looping string, and then find a position such that the entire string starting from that position is lexicographically smallest.

Problem solving ideas:

minimal notation template

Note that the subscript returned by the template starts at 0, and the answer requires it to start at 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 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324887203&siteId=291194637