---
id: 262
lang: en
translation_of: index.md
source_sha256: 1915537a2830b40142b017426cd5e9f4d0087fa1b422dda3ffcf251543ec3c9d
title: "CH1402: Suffix Array — Solution"
description: "Sorting suffixes with a comparator that binary-searches the common prefix length by comparing string hashes."
tags: ["OI","Binary Search","Hashing","String Hashing"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "看着篇文章 read as a typo for 看这篇文章 (“read this post”)."
  - "倍增排序 rendered as “prefix-doubling sort” (the doubling method for building suffix arrays)."
---

## Approach

At first I wrote brute force + prefix-doubling sort, got WA on every single test, and started <span class="slang" data-zh="怀疑人生" data-gloss="huáiyí rénshēng, “doubting life”. Being so baffled or crushed by a result that you start questioning everything.">questioning my life</span>. Then I looked at the intended solution, and sure enough, the binary search is really <span class="slang" data-zh="毒瘤" data-gloss="dúliú, “tumor”. A problem (or problem setter) that is painfully hard, tedious, or full of traps.">nasty</span>.

First prepare the data and write the string hashing. If you aren't sure how to compute string hashes, come read this post: [String hashing example problem](../215-ch1401-rabbit-and-rabbit-string-hash/index.en.html)

<!--more-->

Then, we consider writing a comparison function for the $sort$ function. We can binary search for an equal-prefix length in the interval $[0,min\{len-a+1,len-b+1\}]$, and then the $check(mid)$ function is easy to write: just check directly whether the two hash values are equal.

## Code

```cpp
// CH1402.cpp
#include <iostream>
#include <algorithm>
#define ull long long
using namespace std;

const int maxn = 300000 + 1000;
string str = "";
int idxs[maxn], siz;
ull hashtable[maxn], power[maxn], bitNum = 133;

bool cmp(int a, int b)
{
    int range = min(str.length() - a + 1, str.length() - b + 1);
    int l = 0, r = range, pl = 0;
    while (l <= r)
    {
        int mid = (l + r) >> 1;
        if (hashtable[a + mid - 1] - hashtable[a - 1] * power[mid] == hashtable[b + mid - 1] - hashtable[b - 1] * power[mid])
            l = mid + 1, pl = mid;
        else
            r = mid - 1;
    }
    return str[a + pl] < str[b + pl];
}

int getLen(int a, int b)
{
    int range = min(str.length() - a + 1, str.length() - b + 1);
    int l = 0, r = range, pl = 0;
    while (l <= r)
    {
        int mid = (l + r) >> 1;
        if (hashtable[a + mid - 1] - hashtable[a - 1] * power[mid] == hashtable[b + mid - 1] - hashtable[b - 1] * power[mid])
            l = mid + 1, pl = mid;
        else
            r = mid - 1;
    }
    return pl;
}

int main()
{
    cin >> str;
    siz = str.length();
    str = ' ' + str, power[0] = 1;
    for (int i = 1; i <= siz; i++)
        hashtable[i] = hashtable[i - 1] * bitNum + str[i] - 'a' + 1,
        power[i] = power[i - 1] * bitNum,
        idxs[i] = i;
    sort(idxs + 1, idxs + siz + 1, cmp);
    for (int i = 1; i <= siz; i++)
        printf("%d ", idxs[i] - 1);
    printf("\n0 ");
    for (int i = 2; i <= siz; i++)
        printf("%d ", getLen(idxs[i], idxs[i - 1]));
    return 0;
}
```
