A15952 | Entangled Substrings
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Quantum entanglement is when two particles link together in a certain way no matter how far apart they are in space.
You are given a string $s$ . A pair of its non-empty substrings $(a, b)$ is called entangled if there is a (possibly empty) link string $c$ such that:
- Every occurrence of $a$ in $s$ is immediately followed by $cb$ ;
- Every occurrence of $b$ in $s$ is immediately preceded by $ac$ .
In other words, $a$ and $b$ occur in $s$ only as substrings of $acb$ . Compute the total number of entangled pairs of substrings of $s$ .
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by the deletion of several (possibly zero or all) characters from the beginning and several (possibly zero or all) characters from the end.
You are given a string $s$ . A pair of its non-empty substrings $(a, b)$ is called entangled if there is a (possibly empty) link string $c$ such that:
- Every occurrence of $a$ in $s$ is immediately followed by $cb$ ;
- Every occurrence of $b$ in $s$ is immediately preceded by $ac$ .
In other words, $a$ and $b$ occur in $s$ only as substrings of $acb$ . Compute the total number of entangled pairs of substrings of $s$ .
A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by the deletion of several (possibly zero or all) characters from the beginning and several (possibly zero or all) characters from the end.
输入格式
The first and only line contains a string $s$ of lowercase English letters ( $1 \leq |s| \leq 10^5$ ) — the string for which you should count pairs of entangled substrings.
输出格式
Output a single integer, the number of entangled pairs of substrings of $s$ .
输入输出样例
输入 #1
abba
输出 #1
1
输入 #2
abacaba
输出 #2
0
输入 #3
abcabcabcabc
输出 #3
5
输入 #4
adamant
输出 #4
82
In the first example, the only entangled pair is (ab,ba). For this pair, the corresponding link string $c$ is empty, as they only occur as substrings of the whole string abba, which doesn't have any characters between ab and ba.
In the second example, there are no entangled pairs.
In the third example, the entangled pairs are (a,b), (b,c), (a,c), (a,bc), and (ab,c). For most pairs, the corresponding link string $c$ is empty, except for the pair (a,c), for which the link string $c$ is b, as a and c only occur as substrings of the string abc.
In the second example, there are no entangled pairs.
In the third example, the entangled pairs are (a,b), (b,c), (a,c), (a,bc), and (ab,c). For most pairs, the corresponding link string $c$ is empty, except for the pair (a,c), for which the link string $c$ is b, as a and c only occur as substrings of the string abc.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted