Archives
Trending
Support
Login
clear text
XML
Django
JavaScript
MATLAB
C
C++
C#
Python
SQL
Shell
Bash
Markdown
YAML
JSON
HTML
CSS
PHP
Java
Ruby
Go
Rust
Swift
Kotlin
Arduino
TypeScript
Perl
Autohotkey
Lua
SQF
R
Scala
Haskell
Groovy
Dart
Clojure
VB.NET
Objective-C
PowerShell
Bash
CoffeeScript
Verilog
#include
#include
#include
using namespace std; string pattern, text; const int B = 256; const int MOD = INT_MAX; void robin_carp() { int hash_pattern = 0, hash_text = 0; for(int i = 0; i < (int) pattern.size(); i++) { hash_pattern = (B * hash_pattern + pattern[i]) % MOD; hash_text = (B * hash_text + text[i]) % MOD; } cout << hash_pattern << " " << hash_text << endl; int h = 1; for(int i = 0; i < (int) pattern.size() - 1; i++) { h = (h * B) % MOD; } for(int i = 0; i <= (int) text.size() - (int) pattern.size(); i++) { if(hash_pattern == hash_text) { bool ok = true; for(int j = 0; j < (int) pattern.size(); j++) { if(text[i + j] != pattern[j]) { ok = false; break; } } if(ok) { cout << "found at: " << i << endl; } } if(i < (int) text.size() - (int) pattern.size()) { hash_text = (B * (hash_text - text[i] * h) + text[i + (int) pattern.size()]) % MOD; if(hash_text < 0) { hash_text += MOD; } } } } int main() { ios_base::sync_with_stdio(false); cin >> pattern >> text; robin_carp(); return 0; } /* **/
Mark as private (unlisted)
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
everlasting (like CCCP)