Author : Dipu Kumar Mohanto
CSE, Batch - 6
BRUR.
Problem Statement : 719 - Glass Beads
Source : UVA Online Judge
Category : String
Algorithm : Lexicographically minimul string rotation by Zhou Yuan
Verdict : Accepted
- #include <bits/stdc++.h>
-
- using namespace std;
-
- int minExp(string s)
- {
- s = s + s;
- int i = 0;
- int j = 1;
- int k = 0;
- int len = s.size();
- while (i + k < len && j + k < len)
- {
- if (s[i+k] == s[j+k]) k++;
- else if (s[i+k] < s[j+k])
- {
- j = j + k + 1;
- if (j <= i) j = i + 1;
- k = 0;
- }
- else
- {
- i = i + k + 1;
- if (i <= j) i = j + 1;
- k = 0;
- }
- }
- return min(i, j);
- }
-
- int main()
- {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
-
- int n;
- cin >> n;
- while (n--)
- {
- string s;
- cin >> s;
- int start = minExp(s);
- cout << start+1 << endl;
- }
- }
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.