Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<cstdio>
- #include<cmath>
- using namespace std;
- int mod = 10001;
- int maxlen = 100000;
- int x = 37;
- int hc[100000];
- int exponent(int x, int n) {
- if (n == 0)
- return 1;
- if (n == 1)
- return x;
- if (n == 2)
- return (x*x) % mod;
- int res = exponent(x, n/2) % mod;
- return (res * res * exponent(x, n&1)) % mod;
- }
- void create_hash(char str[]) {
- hc[0] = str[0];
- for (int i = 1; str[i] != '\0'; i++)
- hc[i] = (hc[i-1]*x + str[i]) % mod;
- }
- int hash_get(int i) {
- return hc[i];
- }
- int main() {
- char str[maxlen];
- int loc1, loc2, len;
- int hc1, hc2;
- scanf("%s", &str);
- scanf("%d %d %d", &loc1, &loc2, &len);
- create_hash(str);
- int ex = exponent(x, len);
- if (loc1 == 0)
- hc1 = hash_get(len-1);
- else
- hc1 = (hash_get(loc1+len-1) - hash_get(loc1-1)*ex) % mod;
- if (loc2 == 0)
- hc2 = hash_get(len-1);
- else
- hc2 = (hash_get(loc2+len-1) - hash_get(loc2-1)*ex) % mod;
- int same = 1;
- if (hc1 == hc2) {
- for (int i = 0; i < len; i++)
- if (str[i+loc1] != str[i+loc2]) {
- same = 0;
- break;
- }
- } else
- same = 0;
- printf("%d\n", same);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement