Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long int
- const int mx = 1e5 + 5;
- int p = 31, MOD = 1e9 + 7;
- int pw[mx];
- void pw_cal() {
- pw[0] = 1;
- for (int i = 1; i <= mx; i++) {
- pw[i] = (int) (1LL * pw[i - 1] * p) % MOD;
- }
- }
- int getHash(string s) {
- int res = 0;
- for (int i = 0; i < s.size(); i++) {
- res += 1LL * (s[i] - 'a' + 1) * pw[i] % MOD;
- }
- return res;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- pw_cal();
- string s1, s2;
- cin >> s1 >> s2;
- cout << getHash(s1) << " " << getHash(s2) << endl;
- return 0;
- }
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long int
- const int mx = 1e5 + 5;
- int p1 = 31, p2 = 37, MOD = 1e9 + 9;
- int pw1[mx + 5], pw2[mx + 5];
- void pw_cal() {
- pw1[0] = 1;
- for (int i = 1; i <= mx; i++) {
- pw1[i] = (int) (1LL * pw1[i - 1] * p1) % MOD;
- }
- pw2[0] = 1;
- for (int i = 1; i <= mx; i++) {
- pw2[i] = (int) (1LL * pw2[i - 1] * p2) % MOD;
- }
- }
- pair<int, int> getHash(string s) {
- int res1 = 0;
- for (int i = 0; i < s.size(); i++) {
- res1 += 1LL * (s[i] - 'a' + 1) * pw1[i] % MOD;
- res1%=MOD;
- }
- int res2 = 0;
- for (int i = 0; i < s.size(); i++) {
- res2 += 1LL * (s[i] - 'a' + 1) * pw2[i] % MOD;
- res2%=MOD:
- }
- return {res1, res2};
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- pw_cal();
- string s1, s2;
- cin >> s1 >> s2;
- cout << (getHash(s1) == getHash(s2)) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement