QOJ.ac

QOJ

Time Limit: 3 s Memory Limit: 1024 MB Total points: 100

#4076. 문자열 찾기

统计

对于由英文小写字母组成的两个字符串 $A$ 和 $B$,当满足以下条件时,称这两个字符串“事实上相同”:

  1. $A$ 与 $B$ 的长度相同。
  2. 对所有可能的整数 $i$ 和 $j$,若 $A$ 的第 $i$ 个字符与第 $j$ 个字符相同,则 $B$ 的第 $i$ 个字符与第 $j$ 个字符也相同。
  3. 对所有可能的整数 $i$ 和 $j$,若 $A$ 的第 $i$ 个字符与第 $j$ 个字符不同,则 $B$ 的第 $i$ 个字符与第 $j$ 个字符也不同。

例如,$A=\text{aba}$ 与 $B=\text{pqp}$ 是事实上相同的字符串。但 $A=\text{abca}$ 与 $B=\text{abcb}$ 并不是事实上相同的字符串对。

给定字符串 $T$ 和 $P$,请计算 $T$ 的所有连续子字符串中,与 $P$ 事实上相同的子字符串的个数。

例如,当 $T=\text{abababbab}$,$P=\text{pqp}$ 时,从 $T$ 的左侧开始,$\text{aba}$、$\text{bab}$、$\text{aba}$、$\text{bab}$、$\text{bab}$ 这 5 个子字符串与 $P$ 事实上相同。

你需要编写如下函数:

  • int findP( char T [], char P [], int N, int M )

其中,$T$ 是长度为 $N+1$ 的数组(字符串),$P$ 是长度为 $M+1$ 的数组。$T$ 和 $P$ 中分别存储长度为 $N$ 和 $M$ 的英文小写字母字符串,且它们的最后一个位置存储字符 '\0'findP 需要返回 $T$ 的连续子字符串中,与 $P$ 事实上相同的子字符串的数量。

实现细节

你必须提交且仅提交一个名为 match.cpp 的文件。该文件中需要实现以下函数:

  • int findP( char T [], char P [], int N, int M )

该函数应按上述说明正确工作。当然,你可以在内部创建并使用其他函数。提交的代码不得进行输入输出操作,也不得访问其他文件

输入格式

所提供的 grader 将按照以下格式接收输入:

  • 第 1 行:字符串 $T$
  • 第 2 行:字符串 $P$

grader 会将你在 findP() 函数中返回的值输出为一行。

限制

  • $1 \le N \le 1{,}000{,}000$
  • $1 \le M \le N$

子任务

  • 子任务 1(8 分):$N=M$
  • 子任务 2(5 分):$1 \le N \le 100$
  • 子任务 3(12 分):$1 \le N \le 2{,}000$
  • 子任务 4(75 分):除原始限制条件外无额外限制

样例数据

输入:

abababbab
pqp

在上述输入中,根据代码的执行方式,示例如下:

调用:
findP("abababbab", "pqp", 9, 3)

返回值:
5

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.