rt,原题:
P11705 「KTSC 2020 R1」字符串查找 - 洛谷
我的代码(写了注释是因为想写题解qwq):
#include <bits/stdc++.h>
using namespace std;
int findP(char T[], char P[], int N, int M);
int main(void)
{
char T[1000005], P[1000005];
cin >> T >> P;
int N = strlen(T);
int M = strlen(P);
cout << findP(T, P, N, M);
return 0;
}
int findP(char T[], char P[], int N, int M) {
int cnt = 0;
// 遍历T的每个子字符串
for (int i = 0; i <= N - M; i++) {
unordered_map<char, char> mt, mp;
bool f = true;
// 检查T[i...i+M-1]与P的映射关系
for (int j = 0; j < M; j++) {
char t = T[i + j];
char p = P[j];
// 如果T中的字符t已经映射过其他字符
if (mt.count(t)) {
if (mt[t] != p) {
f = false;
break;
}
} else {
mt[t] = p;
}
// 如果P中的字符p已经映射过其他字符
if (mp.count(p)) {
if (mp[p] != t) {
f = false;
break;
}
} else {
mp[p] = t;
}
}
// 如果映射关系一致,增加计数
if (f) {
cnt++;
}
}
return cnt;
}
这个代码在 IDE 模式里自测样例过了,咋会CE?求救qwq。