HDU5442(字符串的最大表示法或者后缀数组)

题目网址 https://cn.vjudge.net/problem/HDU-5442

 字符串的最大表示法 https://wenku.baidu.com/view/b0ef1be7a6c30c2258019ede.html ,这个博客里面代码https://blog.csdn.net/zy691357966/article/details/39854359

字符串的最大表示:

题解方法转自https://www.cnblogs.com/julyed/p/4812996.html

 

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <string>
#include <cstdio>
#include <cmath>
using namespace std;

int getmx(string a,int n,int f)
{
    int len = a.size();
    int i=0,j=1,k = 0;
    while(i<len&&j<len)
    {
        k = 0;
        while(a[i+k]==a[j+k]&&k<n) k++;
        if(k==n)
        {
            if(!f)
              return min(i,j);
            else  //当翻转时,应该返回下标最大值,这样才对应坐标最小值
            {
                //cout<<" i="<<i<<" j="<<j<<endl;
                int d = abs(i-j);//前缀与后缀相同长度
                return n - d + min(i,j); //取满足要求的最后面位置,即重复后缀的开始位置
            }
        }

        if(a[i+k]<a[j+k])
            i = max(i+k+1,j+1);
        else
            j = max(j+k+1,i+1);
    }
    return min(i,j);
}
int main()
{
    string a;
    string b;
    int T;
    cin>>T;
    while(T--)
    {
        int n;
        cin>>n;
        cin>>a;
        int len = a.size();
        for(int i=0; i<len; i++)
            a += a[i];
        b = a;
        int ans1 = getmx(a,n,0);
        reverse(a.begin(),a.end());
        int ans2 = getmx(a,n,1);
        string aa = b.substr(ans1,n);
        string bb = a.substr(ans2,n);
        //cout<<aa<<" "<<bb<<endl;
        //  cout<<ans1<<" "<<ans2<<endl;
        ans2 = n - ans2 - 1;
        ans1++;
        ans2++;
        if(aa>bb)
            printf("%d 0\n",ans1);
        else if(bb>aa)
            printf("%d 1\n",ans2);
        else
        {
            if(ans1<=ans2)
                printf("%d 0\n",ans1);
            else
                printf("%d 1\n",ans2);
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值