为什么我们在 SPOJ(ALLIZWELL) 中应用 BFS/DFS?为什么简单的蛮力不起作用?
Why do we apply BFS/DFS in SPOJ(ALLIZWELL) ? Why would not simple brute force work?
通过蛮力,我的意思是如果我为字母表 A、L、I、W、E、Z 取 6 个变量 a、l、i、w、e、z 并计算它们的出现次数应用条件为:
if(a<1||l<4||i<1||w<1||e<1||z<2)
{
System.out.println("NO");
}
else
System.out.println("YES");
这有什么问题吗?
这是我的完整代码,而且我得到了错误的答案。
import java.io.*;
public class Main {
public static void main(String asd[]) throws Exception {
Parser in = new Parser(System.in);
int t=in.nextInt();
while(t-->0)
{
int r=in.nextInt();
int c=in.nextInt();int a,l,i,z,w,e;
a=l=i=z=w=e=0;
for(int j=0;j<r;j++)
{
String s=in.next();
for(int k=0;k<s.length();k++)
{
char ch=s.charAt(k);
switch(ch)
{
case 'A':a++;break;
case 'L':l++;break;
case 'I':i++;break;
case 'Z':z++;break;
case 'W':w++;break;
case 'E':e++;break;
}
}
}
if(a<1||l<4||i<1||w<1||e<1||z<2)
{
System.out.println("NO");
}
else
System.out.println("YES");
}
}
}
// for inputting
class Parser {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Parser(InputStream in) {
din = new DataInputStream(in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public long nextLong() throws Exception {
long ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = c == '-';
if (neg) c = read();
do {
ret = ret * 10 + c - '0';
c = read();
} while (c > ' ');
if (neg) return -ret;
return ret;
}
//reads in the next string
public String next() throws Exception {
StringBuilder ret = new StringBuilder();
byte c = read();
while (c <= ' ') c = read();
do {
ret = ret.append((char) c);
c = read();
} while (c > ' ');
return ret.toString();
}
public int nextInt() throws Exception {
int ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = c == '-';
if (neg) c = read();
do {
ret = ret * 10 + c - '0';
c = read();
} while (c > ' ');
if (neg) return -ret;
return ret;
}
private void fillBuffer() throws Exception {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) buffer[0] = -1;
}
private byte read() throws Exception {
if (bufferPointer == bytesRead) fillBuffer();
return buffer[bufferPointer++];
}
}
仅仅因为矩阵包含足够多的字母并不意味着存在将它们全部按正确顺序排列的路径。
举个简单的例子,考虑
艾尔兹威尔
您需要一个与 A 相邻的 L,但是 none。
你玩过这个游戏吗"Boggle?"基本是同一个概念
通过蛮力,我的意思是如果我为字母表 A、L、I、W、E、Z 取 6 个变量 a、l、i、w、e、z 并计算它们的出现次数应用条件为:
if(a<1||l<4||i<1||w<1||e<1||z<2)
{
System.out.println("NO");
}
else
System.out.println("YES");
这有什么问题吗? 这是我的完整代码,而且我得到了错误的答案。
import java.io.*;
public class Main {
public static void main(String asd[]) throws Exception {
Parser in = new Parser(System.in);
int t=in.nextInt();
while(t-->0)
{
int r=in.nextInt();
int c=in.nextInt();int a,l,i,z,w,e;
a=l=i=z=w=e=0;
for(int j=0;j<r;j++)
{
String s=in.next();
for(int k=0;k<s.length();k++)
{
char ch=s.charAt(k);
switch(ch)
{
case 'A':a++;break;
case 'L':l++;break;
case 'I':i++;break;
case 'Z':z++;break;
case 'W':w++;break;
case 'E':e++;break;
}
}
}
if(a<1||l<4||i<1||w<1||e<1||z<2)
{
System.out.println("NO");
}
else
System.out.println("YES");
}
}
}
// for inputting
class Parser {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Parser(InputStream in) {
din = new DataInputStream(in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public long nextLong() throws Exception {
long ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = c == '-';
if (neg) c = read();
do {
ret = ret * 10 + c - '0';
c = read();
} while (c > ' ');
if (neg) return -ret;
return ret;
}
//reads in the next string
public String next() throws Exception {
StringBuilder ret = new StringBuilder();
byte c = read();
while (c <= ' ') c = read();
do {
ret = ret.append((char) c);
c = read();
} while (c > ' ');
return ret.toString();
}
public int nextInt() throws Exception {
int ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = c == '-';
if (neg) c = read();
do {
ret = ret * 10 + c - '0';
c = read();
} while (c > ' ');
if (neg) return -ret;
return ret;
}
private void fillBuffer() throws Exception {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) buffer[0] = -1;
}
private byte read() throws Exception {
if (bufferPointer == bytesRead) fillBuffer();
return buffer[bufferPointer++];
}
}
仅仅因为矩阵包含足够多的字母并不意味着存在将它们全部按正确顺序排列的路径。
举个简单的例子,考虑
艾尔兹威尔
您需要一个与 A 相邻的 L,但是 none。
你玩过这个游戏吗"Boggle?"基本是同一个概念