Warning: session_start(): open(/tmp/sess_fa57ec0703329320d9cd8b30f616ac94, O_RDWR) failed: No space left on device (28) in /data/wiki/inc/init.php on line 239

Warning: session_start(): Failed to read session data: files (path: ) in /data/wiki/inc/init.php on line 239

Warning: Cannot modify header information - headers already sent by (output started at /data/wiki/inc/init.php:239) in /data/wiki/inc/auth.php on line 430

Warning: mkdir(): No space left on device in /data/wiki/lib/plugins/dw2pdf/vendor/mpdf/mpdf/src/Cache.php on line 19
Temporary files directory "/data/wiki/data/tmp/dwpdf/768/" is not writable

Warning: Cannot modify header information - headers already sent by (output started at /data/wiki/inc/init.php:239) in /data/wiki/inc/actions.php on line 38

Warning: Cannot modify header information - headers already sent by (output started at /data/wiki/inc/init.php:239) in /data/wiki/lib/tpl/dokuwiki/main.php on line 12
2020-2021:teams:legal_string:字符串匹配_lgwza [CVBB ACM Team]

用户工具

站点工具


2020-2021:teams:legal_string:字符串匹配_lgwza

# 字符串匹配

## 字符串匹配问题

### 单串匹配

一个模式串(pattern),一个待匹配串,找出前者在后者中的所有出现位置。

### 多串匹配

多个模式串,一个待匹配串(多个待匹配串可以直接连起来)。

直接当作单串匹配肯定是可以的,但是效率不够高。

### 匹配一个串的任意后缀

### 匹配多个串的任意后缀

## 暴力做法

对于每个位置,尝试对模式串和待匹配串进行比对。

参考代码:

(伪代码)

```c++ std::vector<int> match(char *a, char *b, int n, int m) {

std::vector<int> ans;
for (i = 0; i < n - m + 1; i++) {
  for (j = 0; j < m; j++) {
    if (a[i + j] != b[j]) break;
  }
  if (j == m) ans.push_back(i);
}
return ans;

} ```

时间复杂度分析:

最坏时间复杂度是 $O(nm)$ 的,

最好是 $O(n)$ 的。

如果字符集的大小大于 1 (有至少两个不同的字符),平均时间复杂度是 $O(n)$ 的。但是在 OI 题目中,给出的字符串一般都不是纯随机的。

## Hash 的方法

## KMP 算法

## 参考链接

[Oi Wiki](https://oi-wiki.org/string/match/)

2020-2021/teams/legal_string/字符串匹配_lgwza.1594809821.txt.gz · 最后更改: 2020/07/15 18:43 由 lgwza