Wednesday, 23 December 2015

UVa 10855 - Rotated square

//\\__ hr1212 __//\\

import java.io.*;
import java.math.*;
import java.util.*;

public class  Main{

static int i,n,m,z,x,y,j,k,l,r,c1,c2,c3,c4;
static int a[];
static String s,q;
static char p[][],q1[][],q2[][],q3[][],q4[][];
static int MAX=1000010;

public static void main(String[] args) throws IOException{
InputReader in=new InputReader(System.in);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(System.out);

while(true){
n=in.nextInt();m=in.nextInt();
if(n==0 && m==0)
break;
p=new char[1000][1000];
q1=new char[1000][1000];
q2=new char[1000][1000];
q3=new char[1000][1000];
q4=new char[1000][1000];
c1=0;c2=0;c3=0;c4=0;
for(i=0;i<n;i++){
q=in.readString();
for(j=0;j<n;j++)
p[i][j]=q.charAt(j);
}
for(i=0;i<m;i++){
q=in.readString();
for(j=0;j<m;j++)
q1[i][j]=q.charAt(j);
}
l=0;r=m-1;
for(i=0;i<m;i++){
for(j=0;j<m;j++){
q2[l++][r]=q1[i][j];
}
l=0;r--;
}

l=m-1;r=m-1;
for(i=0;i<m;i++){
for(j=0;j<m;j++){
q3[l][r--]=q1[i][j];
}
l--;r=m-1;
}

l=m-1;r=0;
for(i=0;i<m;i++){
for(j=0;j<m;j++){
q4[l--][r]=q1[i][j];
}
l=m-1;r++;
}

for(i=0;i<=n-m;i++){
for(j=0;j<=n-m;j++){
int flag1=0,flag2=0,flag3=0,flag4=0;
for(l=i;l<i+m;l++){
for(r=j;r<j+m;r++){
if(p[l][r]!=q1[l-i][r-j])
flag1=1;
if(p[l][r]!=q2[l-i][r-j])
flag2=1;
if(p[l][r]!=q3[l-i][r-j])
flag3=1;
if(p[l][r]!=q4[l-i][r-j])
flag4=1;
}
}
if(flag1==0)
c1++;
if(flag2==0)
c2++;
if(flag3==0)
c3++;
if(flag4==0)
c4++;

}
}
out.println(c1+" "+c2+" "+c3+" "+c4);
}

out.close();

}
static class InputReader {

private InputStream stream;
private byte[] buf = new byte[8192];
private int curChar;
private int snumChars;
private SpaceCharFilter filter;

public InputReader(InputStream stream) {
this.stream = stream;
}

public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}

public int nextInt() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}

int res = 0;

do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));

return res * sgn;
}

public long nextLong() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}

long res = 0;

do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));

return res * sgn;
}

public String readString() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}

public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}

public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}

No comments:

Post a Comment