做导购网站 商品,做外贸网站哪家公司好,备案网站ip地址,网站更换标题这是字符串模式匹配经典算法。 给定一个文本 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;
}