tinycodinglm / cp2 /cp-0029.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
5.28 kB
cp-practice-sim trace
System You are a competitive-programming assistant. Deliver correct, complete, runnable solutions that read standard input and write standard output. Give only a concise rationale: approach, correctness idea, and time/space com...

You are a competitive-programming assistant. Deliver correct, complete, runnable solutions that read standard input and write standard output. Give only a concise rationale: approach, correctness idea, and time/space complexity.

User 2026-11-08T02:20:03.537Z

Mirror Words Inventory

Topic: String manipulation and parsing | Difficulty: hard

Problem

Count the distinct non-empty palindromic substrings of a string s. Two substrings are the same if they are equal as strings, regardless of where they occur.

Input

A single line containing the string s.

Output

Print the number of distinct palindromic substrings.

Constraints

  • 1 <= |s| <= 2*10^5
  • s consists of lowercase English letters

Examples

Example 1

Input:

markdown
abacaba

Output:

markdown
7

Note: a, b, c, aba, aca, bacab and abacaba.

Example 2

Input:

markdown
aaaa

Output:

markdown
4

Example 3

Input:

markdown
abc

Output:

markdown
3

Solve this in Ruby, Java 17, JavaScript (Node.js), one complete program per language. Include a concise rationale (approach, correctness idea, time/space complexity).

Assistant 2026-11-08T02:20:05.511Z

Approach. Build a palindromic tree (eertree) online: each node is a distinct palindrome, with suffix links to its longest proper palindromic suffix. For every new character walk suffix links from the longest palindromic suffix to find where it extends and add at most one new node.

Correctness. Appending a character creates at most one new distinct palindrome (the longest palindromic suffix of the new prefix), so the number of nodes besides the two roots equals the number of distinct palindromic substrings.

Complexity. Time: O(n). Space: O(26 n).

Ruby

ruby
s = gets.strip.bytes.map { |c| c - 97 }
n = s.size
len = [-1, 0]
link = [0, 0]
nxt = [Array.new(26, 0), Array.new(26, 0)]
last = 1
s.each_with_index do |c, i|
  cur = last
  cur = link[cur] until i - len[cur] - 1 >= 0 && s[i - len[cur] - 1] == c
  if nxt[cur][c] != 0
    last = nxt[cur][c]
    next
  end
  v = len.size
  len << len[cur] + 2
  nxt << Array.new(26, 0)
  if len[v] == 1
    link << 1
  else
    w = link[cur]
    w = link[w] until i - len[w] - 1 >= 0 && s[i - len[w] - 1] == c
    link << nxt[w][c]
  end
  nxt[cur][c] = v
  last = v
end
puts len.size - 2

Java 17

java
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        String s = new BufferedReader(new InputStreamReader(System.in)).readLine().trim();
        int n = s.length();
        int[] len = new int[n + 3];
        int[] link = new int[n + 3];
        int[] nxt = new int[(n + 3) * 26];
        len[0] = -1;
        link[0] = 0;
        len[1] = 0;
        link[1] = 0;
        int cnt = 2, last = 1;
        for (int i = 0; i < n; i++) {
            int c = s.charAt(i) - 'a';
            int cur = last;
            while (!(i - len[cur] - 1 >= 0 && s.charAt(i - len[cur] - 1) - 'a' == c)) cur = link[cur];
            if (nxt[cur * 26 + c] != 0) {
                last = nxt[cur * 26 + c];
                continue;
            }
            int v = cnt++;
            len[v] = len[cur] + 2;
            if (len[v] == 1) {
                link[v] = 1;
            } else {
                int w = link[cur];
                while (!(i - len[w] - 1 >= 0 && s.charAt(i - len[w] - 1) - 'a' == c)) w = link[w];
                link[v] = nxt[w * 26 + c];
            }
            nxt[cur * 26 + c] = v;
            last = v;
        }
        System.out.println(cnt - 2);
    }
}

JavaScript (Node.js)

javascript
const s = require('fs').readFileSync(0, 'utf8').trim();
const n = s.length;
const a = new Uint8Array(n);
for (let i = 0; i < n; i++) a[i] = s.charCodeAt(i) - 97;
const len = new Int32Array(n + 3);
const link = new Int32Array(n + 3);
const nxt = new Int32Array((n + 3) * 26);
len[0] = -1;
let cnt = 2, last = 1;
for (let i = 0; i < n; i++) {
  const c = a[i];
  let cur = last;
  while (!(i - len[cur] - 1 >= 0 && a[i - len[cur] - 1] === c)) cur = link[cur];
  if (nxt[cur * 26 + c] !== 0) {
    last = nxt[cur * 26 + c];
    continue;
  }
  const v = cnt++;
  len[v] = len[cur] + 2;
  if (len[v] === 1) {
    link[v] = 1;
  } else {
    let w = link[cur];
    while (!(i - len[w] - 1 >= 0 && a[i - len[w] - 1] === c)) w = link[w];
    link[v] = nxt[w * 26 + c];
  }
  nxt[cur * 26 + c] = v;
  last = v;
}
console.log(String(cnt - 2));