做挂件的网站,广州网站建设功能,网络营销策划方案框架,西部虚拟主机网站后台不能访问这是字符串模式匹配经典算法。 给定一个文本 t 和一个字符串 s#xff0c;我们尝试找到并展示 s 在 t 中的所有出现#xff08;occurrence#xff09;。 #includebits/stdc.husing namespace std;vectorint KMP(string s) {int n s.size();vectorint我们尝试找到并展示 s 在 t 中的所有出现occurrence。 #includebits/stdc.husing namespace std;vectorint KMP(string s) {int n s.size();vectorint ans(n, -1);int j 0;for(int i 1; i n; i) {j ans[i - 1];while(j 0 s[i] ! s[j]) j ans[j];if(j 0) ans[i] j 1;}return ans;
}int main() {string s1;string s2;cout 主串;cin s1;cout 子串;cin s2;vectorint pi KMP(s2);int a s1.size(), b s2.size();int i 0, j 0;while(i a j b) {if(j -1) {i;j;}if(s1[i] s2[j]) {i;j;} else j pi[j];}if(jb) cout s1 是 s2 的主串 endl;return 0;
}