【洛谷-P1200】解题报告(字符串)

原始题目

P1200 [USACO1.1]你的飞碟在这儿

题目大意

字母'A'~'Z'对应1到26,字符串hash为对应值相乘mod47,若两字符串相等则输出"GO",否则"STAY".

解题思路

字符串hash

解题代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <bits/stdc++.h>
#include <sstream>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<string, string> pss;

#define rep(i, a, n) for (int i = a; i < n; ++i)
#define per(i, a, n) for (int i = n - 1; i >= a; --i)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define np next_permutation
#define INF 0x3f3f3f3f
#define EPS 1e-8
#define endl '\n'
string a, b;
const int mod = 47;
const int maxn = 1e5 + 5;

int main()
{
ios::sync_with_stdio(false);
while (cin >> a >> b) {
int ans = 1, ans2 = 1;
rep(i, 0, a.length())
{
ans = (ans * (a[i] - 'A' + 1)) % mod;
}
rep(i, 0, b.length())
{
ans2 = (ans2 * (b[i] - 'A' + 1)) % mod;
}
if (ans == ans2)
cout << "GO" << endl;
else
cout << "STAY" << endl;
}
}

收获与反思

字符串hash入门