Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

最小表示法 iostream 开加速 wa

Posted by book987 at 2018-04-29 22:19:06 on Problem 1509
// WA
#include <iostream>
using namespace std;

int main(){
    int t; cin >> t; while(t--){
        ios::sync_with_stdio(false); cin.tie(0);
        string s; cin >> s;
        int i = 0, j = 1, k = 0, n = s.length();
        s += s;
        while (i < n && j < n && k < n){
            if (i == j) j++;
            if (s[i + k] == s[j + k]) ++k;
            else s[i + k] > s[j + k] ? i += k + 1 : j += k + 1, k = 0;
        }
        cout << i + 1 << endl;
    }
    return 0;
}

// AC
#include <iostream>
using namespace std;

int main(){
    int t; cin >> t; while(t--){
        string s; cin >> s;
        int i = 0, j = 1, k = 0, n = s.length();
        s += s;
        while (i < n && j < n && k < n){
            if (i == j) j++;
            if (s[i + k] == s[j + k]) ++k;
            else s[i + k] > s[j + k] ? i += k + 1 : j += k + 1, k = 0;
        }
        cout << i + 1 << endl;
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator