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

暴力可以過,1797ms,但是注意幾個优化

Posted by KatrineYang at 2016-11-11 04:26:56 on Problem 1509
1,膜运算尽量少用,最好都提前算出来
2,碰到两个相等的时候直接退出,因为这个时候字符串是周期的
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
using namespace std;


char s[10010];
int l;
int cmp(int a, int b){
	for(int i = 0; i < l; i++){
		int cha = s[(a+i)%l] - s[(b+i)%l];
		if(cha > 0) return 1;
		if(cha < 0) return -1;
	}
	return 0;
}

int main() {
	int n;
	scanf("%d", &n);

	while(n--){
		scanf("%s", s);
		l = strlen(s);
		int mn = 0;
		for(int i = 1; i < l; i++){
			int res = cmp(mn, i);
			if(res > 0) mn = i;
			if(res == 0) break;
		}
		printf("%d\n", mn+1);
	}
	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