ccpc 哈尔滨L题 LRU Algorithm
题目链接
hash加单独判断(边界条件得注意,待匹配串可能是空,又坑了一波队友)
#include<map>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
const int N = 5010;
const int mod1 = 1e9 + 7;
const int mod2 = 1e9 + 9;
const int p = 13331;
#define typeinput int
inline char nc()
{
static char buf[1000000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline void read(typeinput &sum)
{
char ch=nc();
sum=0;
while(!(ch>='0'&&ch<='9')) ch=nc();
while(ch>='0'&&ch<='9') sum=(sum<<3)+(sum<<1)+(ch-48),ch=nc();
}
int a[N],b[N][N],Hash[N][N],x[N];
void init(int n){
for(int i=1;i<=n;i++){
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
Hash[i][j] = b[i][j] = 0;
}
}
}
int main()
{
int T;
read(T);
while(T--){
int n,m;
read(n),read(m);
for(int i=1;i<=n;i++){
read(a[i]);
}
init(n);
int siz = 0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
b[i][j] = b[i-1][j];
}
int pos = 0;
for(int j=1;j<=siz;j++){
if(b[i][j] == a[i]){
pos = j;
}
}
if(pos){
for(int j=pos;j>=1;j--){
b[i][j] = b[i][j-1];
}
}
else {
++siz;
for(int j=siz;j>=1;j--){
b[i][j] = b[i][j-1];
}
}
b[i][1] = a[i];
for(int j=1;j<=n;j++){
Hash[i][j] = (1LL*Hash[i][j-1] * p + b[i][j])%mod1;
}
}
for(int i=1;i<=m;i++){
int L;
read(L);
int hash1 = 0;
for(int j=1;j<=L;j++){
read(x[j]);
}
for(int j=1;j<=L;j++){
hash1 = (1LL*hash1 * p + x[j]) % mod1;
}
int flag = 0;
if(hash1 == 0)
flag = 1;
for(int j=1;j<=n;j++){
if(hash1 == Hash[j][L]){
int flag1 = 1;
for(int k=1;k<=L;k++){
if(x[k]!=b[j][k]){
flag1 = 0;
}
}
if(flag1){
flag = 1;
break;
}
}
}
if(flag){
puts("Yes");
}
else{
puts("No");
}
}
}
return 0;
}