zju1729Hidden Password(最小表示法)

Xredman posted @ Apr 05, 2010 11:00:29 PM in Algorithm with tags ZJU 最小表示法 , 1177 阅读

//2142710  	2010-04-06 09:51:25  	  Accepted  	1729  	C++  	40  	692  	Xredman
#include <iostream>
#include <string>
using namespace std;
/*
用最小表示法求字符串S的最小字典序
*/
int MinRepresstation(string S)
{
	int i = 0, j = 1, k = 0;
	int len = S.length();
	S += S;
	while(i < len && j < len)
	{
		k = 0;
		while(k < len && S[i + k] == S[j + k])
			k++;
		if(k >= len)
			break;
		if(S[i + k] > S[j + k])
			i = max(i + k + 1, j + 1);
		else
			j = max(i + 1, j + k + 1);
	}
	return min(i ,j);
}

int main()
{
	int T;
	string S;
	int beg, k, len;
	cin>>T;
	while(T--)
	{
		cin>>len>>S;
		beg = MinRepresstation(S);
		cout<<beg<<endl;
	}
	return 0;
}

 

 






West Bengal 10th Imp 说:
Sep 01, 2023 11:05:37 PM

West Bengal Board of Secondary Education (WBBSE) Responsibilities Conducting Madhyamaka Examination, WB Board Annual Exam go to Conduct Every year Month of March, WBBSE Regulate, Supervise and Develop Madhyamaka West Bengal 10th Important Questions 2024 (10th) Examination in the State of West Bengal. Students have Tension for the 10th Class Annual Examination 2024. our Website Provided All WBBSE 9th & 10th Class Question Paper 2024 to make Students as Tension Free. Important Question will help the Students for Good Preparation. Students are you Searching for WBBSE 9th & 10th Class Important Question 2024, We will Provide you Previous Question Paper pdf here and also give you Complete information about it.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter