题目链接: 点这里
题意: 略。
思路:hash不会,所以看了题解,学了新东西。
AC代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#define ll long long
#define llu unsigned long long
using namespace std;
const int maxn = 1011130;
const int N = 111123; //121217; //
const double eps = 1e-10;
const double PI = acos(-1.0);
int add[maxn],head[maxn],Next[maxn];
int top;
//vector<int>g[maxn];
int Scan() //输入外挂
{
int res=0,ch,flag=0;
if((ch=getchar())=='-')
flag=1;
else if(ch>='0'&&ch<='9')
res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')
res=res*10+ch-'0';
return flag?-res:res;
}
void Add(int x) {
int tmp = x%N;
add[top] = x;
Next[top] = head[tmp];
head[tmp] = top;
top++;
}
int main(){
int n;
scanf("%d",&n);
char s[10];
memset(head,-1,sizeof(head));
memset(add,0,sizeof(add));
int x;
top=0;
for(int i=1;i<=n;i++) {
scanf("%s%d",s,&x);
int y;
if(s[0] == 'A') {
for(int j=1; j<=x; j++) {
// scanf("%d",&y);
y = Scan();
Add(y);
}
}else {
for(int j=1; j<=x; j++) {
// scanf("%d",&y);
y = Scan();
int tmp = y%N;
bool f = false;
for(int k = head[tmp]; k!=-1; k=Next[k]) {
if(add[k] == y) {
f=true; break;
}
}
if(f) {
printf("YES\n");
}else {
printf("NO\n");
}
}
}
}
return 0;
}
nyoj的测评机感觉有点慢。
在nsoj上 :点这里
AC代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#define ll long long
#define llu unsigned long long
using namespace std;
const int maxn = 211113;
const int N = 121217;
const double eps = 1e-10;
const double PI = acos(-1.0);
int add[maxn];
vector<int>g[maxn];
int main(){
int n;
scanf("%d",&n);
char s[10];
int x;
for(int i=1;i<=n;i++) {
scanf("%s%d",s,&x);
int y;
if(s[0] == 'A') {
for(int j=1; j<=x; j++) {
scanf("%d",&y);
int tmp = y%N;
if(!add[tmp]) {
add[tmp] = 1;
}
g[tmp].push_back(y);
}
}else {
for(int j=1; j<=x; j++) {
scanf("%d",&y);
int tmp = y%N;
bool f = false;
if(add[tmp]) {
for(int k=0;k<g[tmp].size(); k++) {
if(g[tmp][k] == y) {
f = true; break;
}
}
}
if(f) {
printf("YES\n");
}else {
printf("NO\n");
}
}
}
}
return 0;
}
这个代码在nyoj上超时。