忒恶心了,UVa不提示格式错误,格式错误直接WA。。。
C++代码:
#include <cstdio>
#include <utility>
#include <cstring>
using namespace std;
char s[100][100];
int m,n;
int len;
bool searchright(int a,int b,char name[])
{
int i=a,j=b;
if(b+len-1>=n)
return false;
for(;j<=b+len-1;++j)
if(s[i][j]!=name[j-b])
return false;
return true;
}
bool searchleft(int a,int b,char name[])
{
int i=a,j=b;
if(b-len+1<0)
return false;
for(;j>=b-len+1;--j)
if(s[i][j]!=name[b-j])
return false;
return true;
}
bool searchup(int a,int b, char name[])
{
int i=a,j=b;
if(a-len+1<0)
return false;
for(;i>=a-len+1;--i)
if(s[i][j]!=name[a-i])
return false;
return true;
}
bool searchdown(int a,int b, char name[])
{
int i=a,j=b;
if(a+len-1>=m)
return false;
for(;i<=a+len-1;++i)
if(s[i][j]!=name[i-a])
return false;
return true;
}
bool searchleftup(int a,int b,char name[])
{
int i=a,j=b;
if((i-len+1<0)||(j-len+1<0))
return false;
for(;i>=a-len+1;--i,--j)
if(s[i][j]!=name[a-i])
return false;
return true;
}
bool searchrightup(int a,int b,char name[])
{
int i=a,j=b;
if((i-len+1<0)||(b+len-1>=n))
return false;
for(;i>=a-len+1;--i,++j)
if(s[i][j]!=name[a-i])
return false;
return true;
}
bool searchleftdown(int a,int b,char name[])
{
int i=a,j=b;
if((i+len-1>=m)||(j-len+1<0))
return false;
for(;i<=a+len-1;++i,--j)
if(s[i][j]!=name[i-a])
return false;
return true;
}
bool searchrighdown(int a,int b,char name[])
{
int i=a,j=b;
if((i+len-1>=n)||(j+len-1>=n))
return false;
for(;i<=a+len-1;++i,++j)
if(s[i][j]!=name[i-a])
return false;
return true;
}
pair<int,int> search(char name[])
{
int i=0,j=0;
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
if(searchleft(i,j,name)||searchright(i,j,name)||searchup(i,j,name)||searchdown(i,j,name)||
searchleftup(i,j,name)||searchrightup(i,j,name)||searchleftdown(i,j,name)||searchrighdown(i,j,name))
goto loop;
}
}
loop:
return make_pair(i,j);
}
int main()
{
char name[100];
int cas;
while(scanf("%d",&cas)!=EOF)
{
while(cas--)
{
scanf("%d%d",&m,&n);
int i,j;
for(i=0;i<m;++i)
{
scanf("%s",s[i]);
for(j=0;s[i][j];++j)
if(s[i][j]>='A'&&s[i][j]<='Z')
s[i][j]=s[i][j]-'A'+'a';
}
int num;
scanf("%d",&num);
while(num--)
{
scanf("%s",name);
for(i=0;name[i];++i)
if(name[i]>='A'&&name[i]<='Z')
name[i]=name[i]-'A'+'a';
len=strlen(name);
pair<int,int> p=search(name);
printf("%d %d\n",p.first+1,p.second+1);
}
if(cas)
printf("\n");
}
}
return 0;
}