Merge pull request #17 from Kiritow/master

Catch up with master
This commit is contained in:
KiritoTRw 2016-08-13 23:20:55 +08:00 committed by GitHub
commit 39783028c3
20 changed files with 1115 additions and 0 deletions

35
.ACM-Templates/KMP.cpp Normal file
View File

@ -0,0 +1,35 @@
#include <string>
#include <iostream>
#include <cstring>
using namespace std;
void getfill(string s,int* f)
{
//memset(f,0,sizeof(f)); //根据其前一个字母得到
for(size_t i=1;i<s.size();i++)
{
int j=f[i];
while(j && s[i]!=s[j])
j=f[j];
f[i+1]=(s[i]==s[j])?j+1:0;
}
}
int KMP(string a,string s)
{
int* f=new int[s.size()+32];
memset(f,0,sizeof(int)*s.size());
getfill(s,f);size_t j=0;
for(size_t i=0;i<a.size();i++)
{
while(j && a[i]!=s[j])
j=f[j];
if(a[i]==s[j])
j++;
if(j==s.size()){
delete[] f;return i-s.size()+1;
}
}
delete[] f;
return -1;
}

View File

@ -0,0 +1,23 @@
void MakeNext(int* P,int M,int* Next){
Next[0] = -1;
int i = 0, j = -1;
while(i<M){
if(j==-1||P[i]==P[j]){
i++,j++;
if(P[i]!=P[j])Next[i] = j;
else Next[i] = Next[j];
}
else j = Next[j];
}
}
int KMP(int* T,int N,int* P,int M)
{
MakeNext(P,M,Next);
int i=0,j=0;
while(i<N&&j<M){
if(T[i]==P[j]||j==-1)i++,j++;
else j = Next[j];
}
if(j==M)return i-M;
else return -2;
}

2
BNUOJ/Readme.md Normal file
View File

@ -0,0 +1,2 @@
#BNUOJ
[Problem Set](https://www.bnuoj.com/v3/problem.php "Problem List")

View File

@ -0,0 +1,41 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define MAXROUND 100005
double safemoney[MAXROUND];
double roundmoney[MAXROUND];
double p[MAXROUND];
char str[256];
inline double ln(double inc)
{
return log(inc);
}
int main()
{
int n,W;
scanf("%d %d",&n,&W);
for(int i=0;i<n;i++)
{
scanf("%s %lf %lf",str,&p[i],&roundmoney[i+1]);
roundmoney[i+1]=ln(1+roundmoney[i+1]/W);
if(strcmp(str,"safe")==0)
{
safemoney[i+1]=roundmoney[i+1];
}
else
{
safemoney[i+1]=safemoney[i];
}
}
for(int i=n-1;i>=0;i--)
{
double tmp=p[i]*roundmoney[i+1]+(1-p[i])*safemoney[i];
roundmoney[i]=max(tmp,roundmoney[i]);
}
double ans=(exp(roundmoney[0])-1)*W;
printf("$%.2f\n",ans);
}

View File

@ -0,0 +1,38 @@
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
const int MAXN = 100000;
int N, W;
string safe;
long double p[MAXN], v[MAXN+1], s[MAXN+1];
int main()
{
//ios::sync_with_stdio(0);
while (cin >> N >> W && N)
{
v[0] = 0;
for (int i = 0; i < N; ++i)
{
cin >> safe >> p[i] >> v[i+1];
v[i+1] = log1p(v[i+1] / W);
if (safe == "safe")
s[i+1] = v[i+1];
else
s[i+1] = s[i];
}
for (int i = N-1; i >= 0; --i)
{
long double Ewin = p[i]*v[i+1] + (1-p[i])*s[i];
v[i] = max(v[i], Ewin);
}
long double ans = expm1(v[0]) * W;
cout << '$' << fixed << setprecision(2) << ans << endl;
int residualDigit = int(1000*ans) % 10;
if (residualDigit == 4 || residualDigit == 5)
cerr << "WARNING: " << residualDigit << " in the first unseen place" << endl;
}
}

View File

@ -0,0 +1,72 @@
#include <iostream>
using namespace std;
string op;
int parm;
int n;
int operation_type[16];
int operation_parm[16];
bool takeoperation(int a)
{
for(int i=0;i<n;i++)
{
switch(operation_type[i])
{
case 0:
a+=operation_parm[i];
if(a<0) return false;
break;
case 1:
a-=operation_parm[i];
if(a<0) return false;
break;
case 2:
a*=operation_parm[i];
if(a<0) return false;
break;
case 3:
if(a%operation_parm[i]!=0)
{
return false;
}
else
{
a/=operation_parm[i];
}
break;
}
}
return true;
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)
{
cin>>op>>operation_parm[i];
operation_type[i]=0;
switch(op.at(0))
{
case 'D':
operation_type[i]++;
case 'M':
operation_type[i]++;
case 'S':
operation_type[i]++;
case 'A':
break;
}
}
int cnt=0;
for(int i=1;i<=100;i++)
{
if(takeoperation(i))
{
cnt++;
}
}
cout<<100-cnt<<endl;
return 0;
}

View File

@ -0,0 +1,28 @@
#include <iostream>
#include <string>
using namespace std;
string str;
int main()
{
int n,k;
cin>>n>>k;
int x=1;//safe
int y=k;//break
for(int i=0;i<n;i++)
{
int t;
cin>>t>>str;
if(str.at(0)=='B')
{
if(t<y) y=t;
}
else
{
if(t>x) x=t;
}
}
cout<<x+1<<" "<<y-1<<endl;
return 0;
}

View File

@ -0,0 +1,104 @@
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=0,M=0,Z=1e9+7,ms63=1061109567;
int casenum,casei;
int n;
struct A
{
char name[300];
int b[50];
bool operator < (const A& bb)const
{
for(int i=0;i<48;++i)
{
if(b[i]>bb.b[i])return 1;
if(b[i]<bb.b[i])return 0;
}
return strcmp(name,bb.name)<0;
}
}a[1010];
char s[300];
int b[50];
int main()
{
while(~scanf("%d",&n))
{
MS(a,0);
for(int i=1;i<=n;++i)
{
scanf("%s",a[i].name);
int l=strlen(a[i].name);
a[i].name[l-1]=0;
int top=0;
while(1)
{
scanf("%s",s);
if(s[0]=='c')break;
if(s[0]=='u')b[++top]=1;
else if(s[0]=='m')b[++top]=0;
else if(s[0]=='l')b[++top]=-1;
}
int p=0;
while(top)a[i].b[p++]=b[top--];
}
sort(a+1,a+n+1);
for(int i=1;i<=n;++i)printf("%s\n",a[i].name);
}
}
/*
n1<=n<=1000n种难度的课程
n个人的课程难度按照从大到小排序
upper>middle>lower的顺序排序
middle
256/6
upper=1
middle=00
lower=-1
&&
O*nlogn
5
mom: upper upper lower middle class
dad: middle middle lower middle class
queenelizabeth: upper upper class
chair: lower lower class
unclebob: middle lower middle class
*/

18
HDOJ/1071_autoAC.cpp Normal file
View File

@ -0,0 +1,18 @@
#include<stdio.h>
#include<math.h>
int main()
{
int T;
double x1, x2, x3, y1, y2, y3, a, b, c, s;
scanf("%d",&T);
while(T--)
{
scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3);
a = ((y2-y1)*(x3-x2)/(x2-x1)-(y3-y2))/((x2*x2-x1*x1)*(x3-x2)/(x2-x1)-(x3*x3-x2*x2));
b = ((y2-y1)-a*(x2*x2-x1*x1))/(x2-x1);
c = y1-a*x1*x1-b*x1;
s = (a/3*x3*x3*x3+b/2*x3*x3+c*x3)-(a/3*x2*x2*x2+b*x2*x2/2+c*x2)-(y3+y2)*(x3-x2)/2;
printf("%.2lf\n",s);
}
return 0;
}

88
HDOJ/1072_autoAC.cpp Normal file
View File

@ -0,0 +1,88 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <algorithm>
using namespace std;
struct node
{
int x, y;
int step;
int t;
};
const int maxn = 9;
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int maze[maxn][maxn], graph[maxn][maxn];
int n, m, ex, ey, ans;
bool bfs(int x, int y);
int main()
{
int test;
scanf("%d", &test);
while(test-- != 0)
{
scanf("%d %d", &n, &m);
int sx, sy;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
scanf("%d", &maze[i][j]);
if(maze[i][j] == 2)
sx = i, sy = j;
if(maze[i][j] == 3)
ex = i, ey = j;
graph[i][j] = 0;
}
}
if(bfs(sx, sy))
printf("%d\n", ans);
else
printf("-1\n");
}
return 0;
}
bool bfs(int x, int y)
{
queue<node> que;
node s;
s.x = x;
s.y = y;
s.step = 0;
s.t = 6;
graph[x][y] = 6;
que.push(s);
while(!que.empty())
{
node st = que.front();
que.pop();
if(st.x == ex && st.y == ey)
{
ans = st.step;
return true;
}
if(st.t == 1)
continue;
for(int i = 0; i < 4; i++)
{
int dx = st.x + dir[i][0];
int dy = st.y + dir[i][1];
if(dx >= 0 && dx < n && dy >= 0 && dy < m && maze[dx][dy] != 0)
{
node tmp;
tmp.x = dx; tmp.y = dy;
tmp.step = st.step + 1;
tmp.t = st.t - 1;
if(maze[dx][dy] == 4)
tmp.t = 6;
if(tmp.t > graph[dx][dy])
{
graph[dx][dy] = tmp.t;
que.push(tmp);
}
}
}
}
return false;
}

59
HDOJ/1073_autoAC.cpp Normal file
View File

@ -0,0 +1,59 @@
#include<iostream>
#define N 10000
char str1[N],str2[N];
void input(char *str)
{
char tmp[N];
getchar();
gets(tmp);
while(gets(tmp) && strcmp(tmp,"END"))
{
if(strlen(tmp)==0)
strcat(str,"\n");
else
strcat(str,tmp);
}
}
void dechar(char *str,int len)
{
char tmp[N];
int t=0;
for(int i=0;i<len;i++)
if(!(str[i]==' ' || str[i]=='\t' || str[i]=='\n'))
tmp[t++]=str[i];
tmp[t]='\0';
strcpy(str,tmp);
}
int cmp()
{
int n1,n2;
n1=strlen(str1);
n2=strlen(str2);
if(n1==n2 && !strcmp(str1,str2))
return 1;
dechar(str1,n1);
dechar(str2,n2);
if(!strcmp(str1,str2))
return 0;
else
return -1;
}
int main()
{
int t,res;
while(scanf("%d",&t)!=EOF)
{
while(t--)
{
memset(str1,0,sizeof(str1));
memset(str2,0,sizeof(str2));
input(str1);
input(str2);
res=cmp();
if(res==1) puts("Accepted");
else if(res==0) puts("Presentation Error");
else if(res==-1) puts("Wrong Answer");
}
}
return 0;
}

68
HDOJ/1074_autoAC.cpp Normal file
View File

@ -0,0 +1,68 @@
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <iostream>
using namespace std;
const int N = 16;
int dp[1<<N];
struct Node
{
string s;
int time,cost;
};
Node a[N];
int pre[1<<N];
int n;
void Output(int status)
{
if(status==0)return;
int t=0;
for(int i=0;i<n;i++)
if( (status&(1<<i))!=0 && (pre[status]&(1<<i))==0 )
{
t=i;
break;
}
Output(pre[status]);
cout<<a[t].s<<endl;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
cin>>a[i].s>>a[i].time>>a[i].cost;
}
memset(dp,0x3f3f3f3f,sizeof(dp));
memset(pre,0,sizeof(pre));
dp[0]=0;
for(int st=0;st<(1<<n);st++)
{
int tmp=0;
for(int i=0;i<n;i++)
{
if(st&(1<<i))
tmp+=a[i].cost;
}
for(int i=0;i<n;i++)
{
//printf("fff %d %d %d",st,(1<<i),st&(1<<i));
if((st&(1<<i))==0)
{
if(dp[st|(1<<i)]>dp[st]+max(0,tmp+a[i].cost-a[i].time)){
dp[st|(1<<i)]=dp[st]+max(0,tmp+a[i].cost-a[i].time);
pre[st|(1<<i)]=st;
}
}
}
}
printf("%d\n",dp[(1<<n)-1]);
Output((1<<n)-1);
}
return 0;
}

105
HDOJ/1075_autoAC.cpp Normal file
View File

@ -0,0 +1,105 @@
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<ctype.h>
typedef struct node
{
char s[50];
struct node *next[26];
}node;
node memory[1000000];
int k=0;
void insert(char *b,char *a,node *T)
{
int i,len,j,id;
node *p,*q;
p=T;
len=strlen(b);
for(i=0;i<len;++i)
{
id=b[i]-'a';
if(p->next[id]==NULL)
{
q=&memory[k++];
memset(q->s,'\0',sizeof(q->s));
for(j=0;j<26;++j)
q->next[j]=NULL;
p->next[id]=q;
}
p=p->next[id];
}
strcpy(p->s,a);
}
int search(char *t,node *T)
{
int id,i=0;
char *p=t;
node *q=T;
while(islower(p[i]))
{
id=p[i]-'a';
if(q->next[id]==NULL)
return 0;
else
{
q=q->next[id];
++i;
}
}
if(strlen(q->s))
{
printf("%s",q->s);
return 1;
}
return 0;
}
int main()
{
int i,len;
char a[50],b[50];
char c[4000];
node *T;
T=&memory[k++];
memset(T->s,'\0',sizeof(T->s));
for(i=1;i<26;++i)
T->next[i]=NULL;
while(scanf("%s",a)&&strcmp(a,"START")==0)
{
while(scanf("%s",a)&&strcmp(a,"END"))
{
scanf("%s",b);
insert(b,a,T);
}
while(scanf("%s",a)&&strcmp(a,"START")==0)
{
getchar();
while(gets(c))
{
if(strcmp(c,"END")==0)
return 0;
len=strlen(c);
for(i=0;i<len;++i)
{
if(!islower(c[i]))
printf("%c",c[i]);
else
{
if(!search(&c[i],T))
{
while(islower(c[i]))
printf("%c",c[i++]);
--i;
}
else
{
while(islower(c[i++]));
i-=2;
}
}
}
printf("\n");
}
}
}
return 0;
}

31
HDOJ/1076_autoAC.cpp Normal file
View File

@ -0,0 +1,31 @@
#include <stdio.h>
int leapyear(int Y)
{
if((Y%4==0 && Y%100!=0) || (Y%400==0))
return 1;
else
return 0;
}
int main()
{
int Y,N,n,count;
scanf("%d",&n);
while(n--)
{
count=0;
scanf("%d%d",&Y,&N);
while(count<N)
{
if(leapyear(Y))
{
count++;
Y++;
}
else
{
Y++;
}
}
printf("%d\n",Y-1);
}
}

67
HDOJ/1077_autoAC.cpp Normal file
View File

@ -0,0 +1,67 @@
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define INF 1E9
using namespace std;
double X[301],Y[301];
int n,ans;
double x,y;
inline double c(int i)
{
return (X[i]-x)*(X[i]-x)+(Y[i]-y)*(Y[i]-y);
}
int calc()
{
int i,ans=0;
for(i=0;i<n;i++)
{
if(c(i)>1.0+1e-8)continue;
ans++;
}
return ans;
}
void center(int i,int j)
{
double x1=X[i]-X[j],y1=Y[i]-Y[j],len,k;
double a,b;
if(x1==0)
a=1,b=0;
else if (y1==0)
a=0,b=1;
else
{
k=-1/y1*x1;
len=sqrt(1+k*k);
a=1.0/len;
b=k/len;
}
len=1-(x1*x1+y1*y1)/4;
if(len<0)return;
len=sqrt(len);
x1=(X[i]+X[j])/2.0;y1=(Y[i]+Y[j])/2.0;
a*=len;b*=len;
x=x1+a;y=y1+b;
ans=max(ans,calc());
x=x1-a;y=y1-b;
ans=max(ans,calc());
}
int main()
{
int T;
int i,j;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%lf%lf",&X[i],&Y[i]);
ans=0;
for(i=0;i<n;i++)
for(j=i;j<n;j++)
center(i,j);
printf("%d\n",ans);
}
}

61
HDOJ/1078_autoAC.cpp Normal file
View File

@ -0,0 +1,61 @@
#include <iostream>
#include <utility>
using namespace std;
const int N = 102;
int graph[N][N];
int collect[N][N];
int n, k, ans;
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
bool isBound(int x, int y)
{
if(x < 0 || y < 0)
return false;
if(x >= n || y >= n)
return false;
return true;
}
int memory(pair<int, int> p )
{
int i, j, maxb = 0, tmax;
pair<int , int> cs;
if(collect[p.first][p.second] > 0)
return collect[p.first][p.second];
for(i = 0; i < 4; i++)
{
cs = p;
for(j = 0; j < k; j++)
{
cs.first += dir[i][0];
cs.second += dir[i][1];
if(!isBound(cs.first, cs.second))
break;
if(graph[cs.first][cs.second] > graph[p.first][p.second])
{
tmax = memory(cs);
if(tmax > maxb)
maxb = tmax;
}
}
}
collect[p.first][p.second] = maxb + graph[p.first][p.second];
return collect[p.first][p.second];
}
int main()
{
pair<int, int> p;
int i, j;
while(scanf("%d%d", &n, &k))
{
if(n == -1 && k == -1)
break;
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
{
scanf("%d", &graph[i][j]);
collect[i][j] = 0;
}
p.first = p.second = 0;
printf("%d\n", memory(p));
}
return 0;
}

15
HDOJ/1079_autoAC.cpp Normal file
View File

@ -0,0 +1,15 @@
#include <stdio.h>
int main(){
int yy,mm,dd;
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&yy,&mm,&dd);
if( ((mm + dd) & 1) ==0 )
puts("YES");
else if(dd==30 && ( mm == 9 || mm == 11))
puts("YES");
else puts("NO");
}
return 0;
}

66
HDOJ/1080_autoAC.cpp Normal file
View File

@ -0,0 +1,66 @@
#include <stdio.h>
int matrix[5][5] = {
{5, -1, -2, -1, -3},
{-1, 5, -3, -2, -4},
{-2, -3, 5, -2, -2},
{-1, -2, -2, 5, -1},
{-3, -4, -2, -1, 0},
};
char exchange[5] = {'A', 'C', 'G', 'T', ' '};
int Value (char m, char n){
int R, C;
int i;
for (i=0; i<5; ++i){
if (exchange[i] == m)
R = i;
if (exchange[i] == n)
C = i;
}
return matrix[R][C];
}
int Max (int a, int b, int c){
int max = (a > b) ? a : b;
return (max > c) ? max : c;
}
int Similarity (char str1[], int length1, char str2[], int length2){
int dp[110][110];
int i, j;
dp[0][0] = 0;
for (i=1; i<=length1; ++i)
dp[i][0] = dp[i-1][0] + Value (str1[i], ' ');
for (i=1; i<=length2; ++i)
dp[0][i] = dp[0][i-1] + Value (' ', str2[i]);
for (i=1; i<=length1; ++i){
for (j=1; j<=length2; ++j){
if (str1[i] == str2[j]){
dp[i][j] = dp[i-1][j-1] + Value (str1[i], str2[j]);
}
else{
dp[i][j] = Max (dp[i-1][j] + Value (str1[i], ' '),
dp[i][j-1] + Value (' ', str2[j]),
dp[i-1][j-1] + Value (str1[i], str2[j]));
}
}
}
return dp[length1][length2];
}
int main(void){
char str1[110];
char str2[110];
int length1;
int length2;
int T;
int ans;
while (scanf ("%d", &T) != EOF){
while (T-- != 0){
scanf ("%d%s", &length1, str1 + 1);
scanf ("%d%s", &length2, str2 + 1);
if (length1 > length2)
ans = Similarity (str1, length1, str2, length2);
else
ans = Similarity (str2, length2, str1, length1);
printf ("%d\n", ans);
}
}
return 0;
}

100
HDOJ/1247.cpp Normal file
View File

@ -0,0 +1,100 @@
#include <iostream>
#include <cstring>
using namespace std;
class node
{
public:
node* next[26];
bool value;
node() : value(false)
{
memset(next,0,sizeof(next));
}
};
node _headnode;
node* head=&_headnode;
void insert(const char* str)
{
int L=strlen(str);
node* p=head;
for(int i=0;i<L;i++)
{
if(p->next[str[i]-'a']==nullptr)
{
p->next[str[i]-'a']=new node;
}
p=p->next[str[i]-'a'];
}
p->value=true;
}
bool search(const char* str)
{
int L=strlen(str);
node* p=head;
for(int i=0;i<L;i++)
{
if(p->next[str[i]-'a']!=nullptr)
{
p=p->next[str[i]-'a'];
if(p->value)
{
bool isFound=true;
node* q=head;
for(int j=i+1;j<L;j++)
{
if(q->next[str[j]-'a']!=nullptr)
{
q=q->next[str[j]-'a'];
}
else
{
isFound=false;
break;
}
}
if(isFound)
{
if(q->value)
{
return true;
}
}
}
}
else
{
break;
}
}
return false;
}
#define MAXN 50005
#define MAXLEN 1005
char input[MAXN][MAXLEN];
int main()
{
int i=0;
while(cin>>input[i])
{
insert(input[i++]);
}
for(int ci=0;ci<i;ci++)
{
if(search(input[ci]))
{
cout<<input[ci]<<endl;
}
}
return 0;
}

94
HDOJ/1711.cpp Normal file
View File

@ -0,0 +1,94 @@
#include <string>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#define MAXA 1000005
#define MAXN 10010
#define MAXNEXT 10050
/*
int f[MAXN];
void getfill(int* s,int LenS,int* f)
{
//memset(f,0,sizeof(f)); //根据其前一个字母得到
for(int i=1;i<LenS;i++)
{
int j=f[i];
while(j && s[i]!=s[j])
j=f[j];
f[i+1]=(s[i]==s[j])?j+1:0;
}
}
int KMP(int* a,int LenA,int* s,int LenS)
{
f[0]=0;
getfill(s,LenS,f);
int j=0;
for(int i=0;i<LenA;i++)
{
while(j && a[i]!=s[j])
j=f[j];
if(a[i]==s[j])
j++;
if(j==LenS){
return i-LenS+1;
}
}
return -2;
}
//*/
/*** New ***/
int Next[MAXNEXT];
int a[MAXA];
int b[MAXN];
void MakeNext(int* P,int M,int* Next){
Next[0] = -1;
int i = 0, j = -1;
while(i<M){
if(j==-1||P[i]==P[j]){
i++,j++;
if(P[i]!=P[j])Next[i] = j;
else Next[i] = Next[j];
}
else j = Next[j];
}
}
int KMP(int* T,int N,int* P,int M)
{
MakeNext(P,M,Next);
int i=0,j=0;
while(i<N&&j<M){
if(T[i]==P[j]||j==-1)i++,j++;
else j = Next[j];
}
if(j==M)return i-M;
else return -2;
}
/*** End **/
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int sza,szb;
scanf("%d %d",&sza,&szb);
for(int i=0;i<sza;i++)
{
scanf("%d",&a[i]);
}
for(int i=0;i<szb;i++)
{
scanf("%d",&b[i]);
}
printf("%d\n",KMP(a,sza,b,szb)+1);
}
return 0;
}