0% found this document useful (0 votes)
4 views

String

Uploaded by

n23dccn139
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

String

Uploaded by

n23dccn139
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

//1-index

vector<array<int, 26>> nxt(N + 1);


nxt[N].fill(N + 1); // Cài đặt giá trị mặc định là N+1

for (int i = N; i >= 1; --i) {


nxt[i] = nxt[i + 1]; // Di chuyển thông tin tiếp theo từ i+1 sang i+2
nxt[i][S[i] - 'a'] = i; // Chỉnh sửa chỉ số sao cho bắt đầu từ 1
}

1-index chữ cái lần cuối xuất hiện trong trước khi đến chữ cái vị trí i
array<int, 26> last;
fill(last, last + 26, -1);
vector<int> prv(N + 1);
for (int i = 1; i <= N; ++i) {
prv[i] = last[S[i] - 'a'];
last[S[i] - 'a'] = i ;
}

KMP
vector<int> kmp(string s) {
int n = (int)s.length();
vector<int> pi(n);
for (int i = 1; i < n; i++) {
int j = pi[i - 1];
while (j > 0 && s[i] != s[j])
j = pi[j - 1];
if (s[i] == s[j])
j++;
pi[i] = j;
}
return pi;
}

Z-function
vector<int> z_function(string s) {
int n = s.length();
vector<int> z(n);
for (int i = 1, l = 0, r = 0; i < n; ++i) {
// khoi tao z[i] theo thuat toan toi uu
if (i <= r)
z[i] = min(r - i + 1, z[i - l]);
// tinh z[i]
while (i + z[i] < n && s[z[i]] == s[i + z[i]])
++z[i];
// cap nhat doan [l,r]
if (i + z[i] - 1 > r) {
l = i;
r = i + z[i] - 1;
}
}
return z;
}

TRIE
class Trie {
struct Node {
Node* child[26];
int exist;
int cnt;

Node() : exist(0), cnt(0) {


for (int i = 0; i < 26; ++i) {
child[i] = nullptr;
}
}
};

public:
Trie() : root(new Node()) {}

void erase(string word) {


if (!searchWord(word)) return;
deleteWord(root, word, 0);
}

bool deleteWord(Node*& p, const string& str, int i) {


if (i == str.size()) {
p->exist--;
} else {
char c = str[i];
int idx = c - 'a';
if (p->child[idx] && deleteWord(p->child[idx], str, i + 1)) {
delete p->child[idx];
p->child[idx] = nullptr;
}
}

if (p->cnt == 0 && p->exist == 0) {


for (int j = 0; j < 26; ++j) {
if (p->child[j]) return false;
}
return true;
}
p->cnt--;
return false;
}

void insertWord(string &word) {


Node* p = root;
for (char &c : word) {
int idx = c - 'a';
if (!p->child[idx]) p->child[idx] = new Node();
p = p->child[idx];
p->cnt++;
}
p->exist++;
}

bool searchWord(string &word) {


Node* p = root;
for (char &c : word) {
int idx = c - 'a';
if (!p->child[idx]) return false;
p = p->child[idx];
}
return p->exist > 0;
}

private:
Node* root;
};

const int base = 31;


const ll MOD = 1000000003;
const ll maxn = 1000111;
ll POW[maxn], hashT[maxn];

ll getHashT(int i,int j) return (hashT[j] - hashT[i - 1] * POW[j - i + 1] + MOD * MOD) % MOD;

Chuỗi sau khi nối tương s = s1(hash1) + s2(hash2)


ll mergeHash(ll hash1, int len1, ll hash2, int len2) return (hash1 * POW[len2] + hash2) % MOD;

int main() {
string T, P;
cin >> T >> P;

int lenT = T.size(), lenP = P.size();


T = " " + T;
P = " " + P;
POW[0] = 1;

for (int i = 1; i <= lenT; i++)


POW[i] = (POW[i - 1] * base) % MOD;

for (int i = 1; i <= lenT; i++)


hashT[i] = (hashT[i - 1] * base + T[i] - 'a' + 1) % MOD;

ll hashP=0;
for (int i = 1; i <= lenP; i++)
hashP = (hashP * base + P[i] - 'a' + 1) % MOD;

// Finding substrings of T equal to string P


for (int i = 1; i <= lenT - lenP + 1; i++)
if (hashP == getHashT(i, i + lenP - 1))
printf("%d ", i);
}

You might also like