Created
October 20, 2016 18:46
-
-
Save own2pwn/eda6350cde0c28791baac807dd367ea6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main() { | |
string what,where; | |
cin >> what >> where; | |
int ret = 0, whatLen = what.length(), whereLen = where.length(); | |
vector<int> res(whatLen); | |
res[0] = 0; | |
for (int k = 0, i = 1; i < whatLen; i++){ | |
while(k > 0 && what[i] != what[k]) | |
k = res[k-1]; | |
if (what[i] == what[k]) | |
k++; | |
res[i] = k; | |
} | |
for (int k = 0, i = 0; i < whereLen; ++i){ | |
while ((k>0) && (what[k] != where[i])) | |
k = res[k-1]; | |
if (what[k] == where[i]) | |
k++; | |
if (k == whatLen) | |
ret += (i- whatLen +1); | |
} | |
std::cout << ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment