P4173 殘缺的字串 FFT字串匹配

2022-06-22 21:30:13 字數 2230 閱讀 1484

p4173 殘缺的字串(fft字串匹配)

p4173

經典套路將模式串翻轉,將*設為0,設以目標串的x位置匹配結束的匹配函式為\(p(x)=\sum^_[a(m-1-i)-b(x-(m-1-i))]^2a(m-1-i)b(x-(m-1-i))]\),展開之後化簡為\(p(x)=\sum_a^3(i)b(j)-2\sum_a^2(i)b^2(j)+\sum_a(i)b^3(j)\)

做三次fft即可,然後交題就出了一堆玄學錯誤

#include using namespace std;

/* freopen("k.in", "r", stdin);

freopen("k.out", "w", stdout); */

//clock_t c1 = clock();

//std::cerr << "time:" << clock() - c1 <<"ms" << std::endl;

//#pragma comment(linker, "/stack:1024000000,1024000000")

#define de(a) cout << #a << " = " << a << endl

#define rep(i, a, n) for (int i = a; i <= n; i++)

#define per(i, a, n) for (int i = n; i >= a; i--)

typedef long long ll;

typedef unsigned long long ull;

typedef pairpii;

typedef pairpdd;

typedef vectorvii;

#define inf 0x3f3f3f3f

const ll inf = 0x3f3f3f3f3f3f3f3f;

const ll maxn = 2e6 + 10;

const ll maxm = 5e6 + 7;

const ll mod = 1e9 + 7;

const double eps = 1e-6;

const double pi = acos(-1.0);

struct complex

} a[maxn], b[maxn], c[maxn];

complex operator+(complex a, complex b)

complex operator-(complex a, complex b)

complex operator*(complex a, complex b) //不懂的看複數的運算那部分

int l = 0, r[maxn];

int limit = 1;

void fft(complex *a, int type)}}

/* if (type == -1)

for (int i = 0; i < limit; i++)

a[i].x /= limit; */

}char s[maxn], t[maxn];

int ta[maxn] = , tb[maxn] = ;

int ans[maxn] = ;

int main()

fft(a, 1), fft(b, 1);

for (int i = 0; i < limit; i++)

c[i] = c[i] + a[i] * b[i];

for (int i = 0; i < limit; i++)

fft(a, 1), fft(b, 1);

for (int i = 0; i < limit; i++)

c[i] = c[i] - a[i] * b[i] * complex(2.0, 0);

for (int i = 0; i < limit; i++)

fft(a, 1), fft(b, 1);

for (int i = 0; i < limit; i++)

c[i] = c[i] + a[i] * b[i];

fft(c, -1);

int cnt = 0;

for (int i = m - 1; i < n; i++)

printf("%d\n", cnt);

for (int i = 0; i < cnt; i++)

printf("%d ", ans[i]);

printf("\n");

return 0;

}

P4173 殘缺的字串 FFT

給出兩個字串 s,t 其中包含小寫字母和一些 可以匹配任何字元。求有多少個 p 使得 t s 如果不考慮 我們可以用做差法來匹配兩個字元,構造匹配函式 f x sum t i s 2 這樣若 f x 0 證明它們在位置 x 處匹配。但是現在有 也就是要跳過有 的位置,定義 的值為 0 然後改一下匹配...

洛谷 P4173 殘缺的字串 FFT

給定長度為 m 的模式串和長度為 n 的目標串,兩個串都帶有萬用字元,求所有匹配的位置。fft 帶有萬用字元的字串匹配問題。設模式串為 p 目標串為 t 將兩個串的內容都根據字母先後順序對映到 1 到 26 如果不帶有萬用字元,那麼 t 以第 k 位結束的長度為 p 的子串與 p 匹配時有 sum ...

洛谷 P4173 殘缺的字串

不知道xjb kmp可不可以做的說 假設下標都以0開頭 對於有一定偏移量的序列的 對應位置 匹配或者數值計算的題,這裡是有一種套路的,就是把其中乙個序列翻轉過來,然後卷積一下,所得到的新序列c的每乙個位置就包含了 所有原來一定偏移量的位置的乘積和。對於這個題,我們只需要找到一種方法,使相同的字元代表...