于是他错误的点名开始了
#include <bits/stdc++.h>
using namespace std;
map<string,int>tt;
int main ()
{
int n, m;
cin >> n;
string a;
while(n --){
cin >> a;
tt[a] = 1;
}
cin >> m;
while(m --){
string b;
cin >> b;
if(tt[b] == 1){
cout << "OK" << endl;
tt[b] = 2;
}
else if(tt[b] == 2){
cout << "REPEAT" << endl;
}
else {
cout << "WRONG" << endl;
}
}
return 0;
}
这里我用到了map函数
用数组来记录每个名字出现的次数 最后得出答案
A-B 数对
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
map<ll,ll>a;
int main ()
{
ll b[10000001];
ll n, c, ans;
ans = 0;
cin >> n >> c;
for(int i = 1;i <= n;i++){
cin >> b[i];
a[b[i]] ++;
}
for(int i = 1;i <= n;i++){
ans += a[b[i] - c];
}
cout << ans;
return 0;
}
这里有运用到了map函数
记录每个数字出现的次数
既然是减法
那就求出相差c的数的个数
【模板】字符串哈希
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ull base = 131;
ull a[10010];
char s[10010];
int n,ans = 1;
ull hashs(char s[])
{
int len = strlen(s);
ull ans = 0;
for (int i = 0;i < len;i ++)
ans = ans * base + (ull)s[i];
return ans&0x7fffffff; //0x7fffffff 代表int的最大值
}
int main()
{
scanf("%d",&n);
for (int i = 1;i <= n;i++)
{
scanf("%s",s);
a[i] = hashs(s);
}
sort(a + 1,a + n + 1);
for (int i = 2;i <= n;i ++)
if (a[i] != a[i - 1])
ans ++;
printf("%d\n",ans);
}
这里运用了哈希表
把输进去的每个值用一个唯一的数代替 最后记录在一个数组中
在给数组进行排序
把相邻的不相等的数一一数出来 得答案