题目大意:输出第一个字符串的最小字典序字串的下标!

然后我居然想试一试string的erase的能力,暴力一下,然后20msAC了,尴尬的数据。。。。。。。。。。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<string>
using namespace std;
string s,Min;
char temp;
int Pos;
void _work()
{
	cin>>s;
	Min=s;
	Pos=1;
	int L=s.length();
	  for(int i=2;i<=L;i++){
	    temp=s[0];
	    s.erase(s.begin());
	    s+=temp;
	    if(s<Min) {Min=s;Pos=i;}
    }
    printf("%d\n",Pos);
	
}
int main()
{
	int T;
	cin>>T;
    while(T--) _work();
	return 0;
}

当然正解也简单。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 200017;
char str[maxn], tmp[maxn];
//最小表示法
int len;
int _minstring(char *s)
{
    int len = strlen(s);
    int i = 0, j = 1, k = 0;
    while(i<len && j<len && k<len)
    {
        int t=s[(i+k)%len]-s[(j+k)%len];
        if(t==0)
            k++;
        else
        {
            if(t > 0)
                i+=k+1;
            else
                j+=k+1;
            if(i==j) j++;
            k=0;
        }
    }
    return min(i,j);
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        //scanf("%d",&len);
        scanf("%s",str);
        int ans=_minstring(str);
        cout<<ans+1<<endl;
    }
    return 0;
}


0
0
 
 

相关文章:

  • 2021-10-05
  • 2021-09-06
  • 2021-11-01
  • 2022-12-23
  • 2022-12-23
  • 2021-05-04
  • 2021-12-03
  • 2021-05-31
猜你喜欢
  • 2021-07-10
  • 2022-12-23
  • 2022-02-27
  • 2021-07-18
  • 2022-12-23
相关资源
相似解决方案