题解:POJ1743 Musical Theme 【后缀数组】

传送门

这个题目的题意就是差分一下,求差分数组的不可重叠的最长重复子串

这个题目的暴力很简单,就不多扯了(考试打了四个暴力的蒟蒻捂脸走开

于是我们考虑二分这个子串的长度,然后运用后缀数组进行判断

#include <algorithm>
#include <iostream>
#include <cstdio>
using namespace std;

const int N=202000;
int n,m,sa[N],h[N],rk[N],s[N],a[N],x[N],y[N],c[N];

struct Sa {
    void qsort() {
        for(int i=1; i<=m; ++i)
            c[i]=0;
        for(int i=1; i<=n; ++i)
            ++c[x[i]];
        for(int i=1; i<=m; ++i)
            c[i]+=c[i-1];
        for(int i=n; i>=1; --i)
            sa[c[x[y[i]]]--]=y[i];
    }
    void get_sa() {
        for(int i=1; i<=n; ++i) 
            x[i]=s[i],y[i]=i;
        qsort(); 
        for(int k=1,num; num!=n; k<<=1,m=num) {
            num=0;
            for(int i=n-k+1; i<=n; ++i)
                y[++num]=i;
            for(int i=1; i<=n; ++i)
                if(sa[i]>k)
                    y[++num]=sa[i]-k;
            qsort();
            swap(x,y),num=0;
            for(int i=1; i<=n; ++i)
                x[sa[i]]=(y[sa[i]]==y[sa[i-1]])&&(y[sa[i]+k]==y[sa[i-1]+k])?num:++num;
        }
        for(int i=1; i<=n; ++i)
            rk[sa[i]]=i;
    }
    void get_head() {
        int k=0;
        for(int i=1; i<=n; ++i) {
            if(rk[i]==1) continue;
            if(k) --k;
            int j=sa[rk[i]-1];
            while(i+k<=n&&j+k<=n&&s[i+k]==s[j+k])
                ++k;
            h[rk[i]]=k;
        }
    }
}SA;

int nmax,nmin,ans;

bool check(int x) {
    nmax=-10000;
    nmin=10000;
    for(int i=1; i<=n; i++) {
        if (h[i]>=x) {
            if(nmin==10000) {
                nmax=max(sa[i-1],nmax);
                nmin=min(sa[i-1],nmin);
            }
            nmax=max(sa[i],nmax);
            nmin=min(sa[i],nmin);
            if(nmax-nmin>x) return 1;
        } else nmax=-10000,nmin=10000;
    }
    return 0;
}

inline int read() {
    int s=0;
    char ch=getchar();
    while(ch<'0'||ch>'9') ch=getchar();
    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
    return s;
}
int main() {

    n=read(),m=200;
    for(int i=1; i<=n; i++)
        a[i]=read();
    for(int i=1; i<=n; i++)
        s[i]=a[i]-a[i-1]+100;
    SA.get_sa(),SA.get_head();
    int l=1,r=n,mid;
    while(mid=l+r>>1,l^r)
        if(check(mid+1)) ans=l=mid+1;
        else r=mid;
    if(ans+1>=5)  cout<<ans+1;
    else cout<<0;

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!对于扫雷游戏的题解,我可以给你一些思路和代码示例。首先,你需要了解扫雷游戏的规则和要求。接下来,你可以使用C++语言来实现游戏逻辑和界面。 下面是一个简单的扫雷游戏的C++代码示例: ```cpp #include <iostream> #include <vector> #include <random> using namespace std; class MinesweeperGame { private: int rows; int cols; vector<vector<char>> board; vector<vector<bool>> revealed; vector<pair<int, int>> directions = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}; public: MinesweeperGame(int m, int n, int mineCount) { rows = m; cols = n; board.resize(rows, vector<char>(cols, ' ')); revealed.resize(rows, vector<bool>(cols, false)); placeMines(mineCount); calculateNumbers(); } void printBoard() { cout << " "; for (int j = 0; j < cols; j++) { cout << j << " "; } cout << endl; for (int i = 0; i < rows; i++) { cout << i << " |"; for (int j = 0; j < cols; j++) { cout << board[i][j] << "|"; } cout << endl; } } bool isGameOver() { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (board[i][j] == 'M' && revealed[i][j]) { return true; } } } return false; } void reveal(int row, int col) { if (row < 0 || row >= rows || col < 0 || col >= cols || revealed[row][col]) { return; } revealed[row][col] = true; if (board[row][col] == 'M') { return; } if (board[row][col] == '0') { for (auto dir : directions) { reveal(row + dir.first, col + dir.second); } } } private: void placeMines(int mineCount) { random_device rd; mt1

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值