SystemYou 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. Tools: write_file(path, content) writes a UTF-8 file (creating directories); run_shell(command) runs a bash command and returns its output and exit status. Test solutions on the provided examples before answering.
User2026-11-08T11:20:03.537Z
Mirror Pairs
Topic: Hashing and counting (maps/sets) | Difficulty: hard
Problem
You are given n non-empty lowercase strings s_1..s_n (equal strings may appear several times and are different items). Count ordered pairs (i, j) with i != j such that the concatenation s_i + s_j is a palindrome.
Input
The first line contains n. Each of the next n lines contains one string.
Note: abccba and cbaabc are palindromes; x pairs with nothing.
Solve this task. Write solutions in Kotlin, C++17, Go, test what you can run on the examples, and send back the final programs. Include a concise rationale (approach, correctness idea, time/space complexity).
Assistant2026-11-08T11:20:05.511Z
Writing the Kotlin solution and testing it on the examples.
Assistant2026-11-08T11:20:09.870Z
Saving the example inputs.
3 tool calls (write_file)
Assistant2026-11-08T11:20:18.432Z
Compiling the Kotlin solution.
Assistant2026-11-08T11:20:22.635Z
Running every example.
3 tool calls (run_shell)
Assistant2026-11-08T11:20:31.785Z
Cross-checking with the C++17 version.
Assistant2026-11-08T11:20:35.832Z
Compiling the C++17 solution.
Assistant2026-11-08T11:20:40.427Z
Running every example.
3 tool calls (run_shell)
Assistant2026-11-08T11:20:48.561Z
Cross-checking with the Go version.
Assistant2026-11-08T11:20:53.000Z
Compiling the Go solution.
Assistant2026-11-08T11:20:57.987Z
Running every example.
3 tool calls (run_shell)
Assistant2026-11-08T11:21:06.005Z
Approach. Put hashes of all strings in a count map. For a string x acting as the left part, every split x = rev(y) + p with p a palindromic suffix gives partners y; for x acting as the right part, every split x = q + rev(z) with q a nonempty palindromic proper prefix gives partners z. Palindrome tests and reversed-substring hashes come from forward and reverse prefix hashes, and self-pairs of palindromic strings are subtracted.
Correctness. If |s_i| >= |s_j| then s_i + s_j is a palindrome iff s_i = rev(s_j) + p with p a palindrome; otherwise iff s_j = q + rev(s_i) with q a nonempty palindrome. The two cases are disjoint by length and each valid pair is found exactly once.
Complexity. Time: O(L + n). Space: O(L).
All solutions below passed every example.
Kotlin
kotlin
import java.io.*
constval M1 = 1000000007Lconstval M2 = 998244353Lconstval B1 = 131Lconstval B2 = 137Lfunmain() {
val br = BufferedReader(InputStreamReader(System.`in`))
val n = br.readLine().trim().toInt()
val p1 = LongArray(200005)
val p2 = LongArray(200005)
p1[0] = 1; p2[0] = 1for (i in1 until p1.size) { p1[i] = p1[i - 1] * B1 % M1; p2[i] = p2[i - 1] * B2 % M2 }
funbuild(x: String): Array<LongArray> {
val f1 = LongArray(x.length + 1)
val f2 = LongArray(x.length + 1)
for (i in x.indices) {
f1[i + 1] = (f1[i] * B1 + x[i].code) % M1
f2[i + 1] = (f2[i] * B2 + x[i].code) % M2
}
return arrayOf(f1, f2)
}
funsub(f: Array<LongArray>, l: Int, r: Int): Long {
val a = ((f[0][r] - f[0][l] * p1[r - l]) % M1 + M1) % M1
val b = ((f[1][r] - f[1][l] * p2[r - l]) % M2 + M2) % M2
return a * M2 + b
}
val s = Array(n) { br.readLine().trim() }
val cnt = HashMap<Long, Long>()
for (x in s) {
val k = sub(build(x), 0, x.length)
cnt[k] = (cnt[k] ?: 0L) + 1
}
var ans = 0Lvar pal = 0Lfor (x in s) {
val m = x.length
val f = build(x)
val g = build(x.reversed())
funrevh(l: Int, r: Int) = sub(g, m - r, m - l)
funispal(l: Int, r: Int) = sub(f, l, r) == revh(l, r)
for (k in1..m) if (ispal(k, m)) ans += cnt[revh(0, k)] ?: 0Lif (ispal(0, m)) pal++
for (k in1 until m) if (ispal(0, k)) ans += cnt[revh(k, m)] ?: 0L
}
println(ans - pal)
}
C++17
cpp
#include<bits/stdc++.h>usingnamespace std;
typedefunsignedlonglong ull;
const ull M1 = 1000000007ULL, M2 = 998244353ULL, B1 = 131, B2 = 137;
intmain(){
int n;
scanf("%d", &n);
vector<string> s(n);
char buf[200005];
for (auto &x : s) { scanf("%s", buf); x = buf; }
vector<ull> p1(200005), p2(200005);
p1[0] = p2[0] = 1;
for (int i = 1; i < 200005; i++) { p1[i] = p1[i-1] * B1 % M1; p2[i] = p2[i-1] * B2 % M2; }
auto build = [&](const string &x, vector<ull> &f1, vector<ull> &f2) {
int m = x.size();
f1.assign(m + 1, 0); f2.assign(m + 1, 0);
for (int i = 0; i < m; i++) {
f1[i+1] = (f1[i] * B1 + x[i]) % M1;
f2[i+1] = (f2[i] * B2 + x[i]) % M2;
}
};
auto sub = [&](const vector<ull> &f1, const vector<ull> &f2, int l, int r) {
ull a = (f1[r] + M1 * M1 - f1[l] * p1[r-l]) % M1;
ull b = (f2[r] + M2 * M2 - f2[l] * p2[r-l]) % M2;
return a * M2 + b;
};
unordered_map<ull, longlong> cnt;
vector<ull> f1, f2, g1, g2;
for (auto &x : s) {
build(x, f1, f2);
int m = x.size();
cnt[sub(f1, f2, 0, m)]++;
}
longlong ans = 0, pal = 0;
for (auto &x : s) {
int m = x.size();
string r(x.rbegin(), x.rend());
build(x, f1, f2);
build(r, g1, g2);
// hash of reversed x[l:r] = forward hash of r[m-r' .. m-l]auto revh = [&](int l, int rr) { returnsub(g1, g2, m - rr, m - l); };
auto ispal = [&](int l, int rr) { returnsub(f1, f2, l, rr) == revh(l, rr); };
for (int k = 1; k <= m; k++) {
if (ispal(k, m)) {
auto it = cnt.find(revh(0, k));
if (it != cnt.end()) ans += it->second;
}
}
if (ispal(0, m)) pal++;
for (int k = 1; k < m; k++) {
if (ispal(0, k)) {
auto it = cnt.find(revh(k, m));
if (it != cnt.end()) ans += it->second;
}
}
}
printf("%lld\n", ans - pal);
}
Go
go
package main
import (
"bufio""fmt""os"
)
const M1, M2, B1, B2 = 1000000007, 998244353, 131, 137var p1, p2 [200005]uint64funcbuild(x []byte) ([]uint64, []uint64) {
f1 := make([]uint64, len(x)+1)
f2 := make([]uint64, len(x)+1)
for i, c := range x {
f1[i+1] = (f1[i]*B1 + uint64(c)) % M1
f2[i+1] = (f2[i]*B2 + uint64(c)) % M2
}
return f1, f2
}
funcsub(f1, f2 []uint64, l, r int)uint64 {
a := (f1[r] + M1*M1 - f1[l]*p1[r-l]) % M1
b := (f2[r] + M2*M2 - f2[l]*p2[r-l]) % M2
return a*M2 + b
}
funcmain() {
rd := bufio.NewReaderSize(os.Stdin, 1<<20)
var n int
fmt.Fscan(rd, &n)
p1[0], p2[0] = 1, 1for i := 1; i < len(p1); i++ {
p1[i] = p1[i-1] * B1 % M1
p2[i] = p2[i-1] * B2 % M2
}
s := make([][]byte, n)
cnt := map[uint64]int64{}
for i := range s {
var t string
fmt.Fscan(rd, &t)
s[i] = []byte(t)
f1, f2 := build(s[i])
cnt[sub(f1, f2, 0, len(t))]++
}
var ans, pal int64for _, x := range s {
m := len(x)
r := make([]byte, m)
for i := range x {
r[m-1-i] = x[i]
}
f1, f2 := build(x)
g1, g2 := build(r)
revh := func(l, rr int)uint64 { return sub(g1, g2, m-rr, m-l) }
ispal := func(l, rr int)bool { return sub(f1, f2, l, rr) == revh(l, rr) }
for k := 1; k <= m; k++ {
if ispal(k, m) {
ans += cnt[revh(0, k)]
}
}
if ispal(0, m) {
pal++
}
for k := 1; k < m; k++ {
if ispal(0, k) {
ans += cnt[revh(k, m)]
}
}
}
fmt.Println(ans - pal)
}