首页 > 专题系列 > Java解POJ > POJ 1800 Magic Trick [解题报告] Java
2013
11-10

POJ 1800 Magic Trick [解题报告] Java

Magic Trick

问题描述 :

Background

Warning! This problem statement contains a serious spoiler. It shows the trick behind a magic trick. So if you still want to be amazed in case somebody shows this trick to you then do NOT read the rest of this problem statement. Stop reading… NOW!

Problem

Well, you’re still reading, so obviously you have no respect for magic tricks. Be ashamed, please. Ok,here’s what happens. The magician shows you a text with three paragraphs like this one:

It was a horribly dark night.

The moon was shining, but not much.

A suspicious stranger entered the

bar and went straight to John Doe.

“I’m searching for aliens, can I

borrow your computer?”, he said.

He then asks you to secretly pick a word in the first paragraph. Then you shall do this:

1. Count the number of characters in your word (call that number X).

2. From your word move on X words.

Repeat these two steps until you reach the third paragraph. Then tell the magician that you’re done.After some hocus pocus he tells you the word you ended up with.

For our purposes, a “word” is defined as consecutive letters (A-Z,a-z). For example, “I’m” is regarded as two separate words.

For example, let’s say you choose “night” in the above example. It has 5 characters, so you move on five words: “The”, “moon”, “was”, “shining”, “but”. Our new word is “but”. You move on 3 words to “A”,then 1 to “suspicious”, then 10 to “Doe” and then 3 to “searching”. Now you tell the magician that you’re ready. He says that you’ve reached “searching”.

How can he know? Well, it doesn’t matter where you start in the first paragraph, you’ll always end up at “searching”. The magician needs new texts and asks you to help him to find all possible outcomes (in the above example, “searching” is the only one). Apart from words, a possible outcome is “-outside-”,which means it’s possible to jump behind the third paragraph. Also, he’s not interested if more than three outcomes are possible.

输入:

The first line contains the number of scenarios. For each scenario, three lines are given, representing the three paragraphs. No line is longer than 100000 characters. Every paragraph will contain at least one word.

输出:

The output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then print the possible outcomes (possibly including “-outside-”) in alphabetical/lexicographical order, one word per line. Write words in lower case. Don’t list outcomes more than once. If however there are more than three possible outcomes, then print “-too many-” and do *not* print any of them. Terminate the output for the scenario with a blank line.

样例输入:

4
It was a horribly dark night. The moon was shining, but not much.
A suspicious stranger entered the bar and went straight to John Doe.
"I'm searching for aliens, can I borrow your computer?", he said.
!pablo espanol!
!pablo espanol!
!pablo espanol!
c'mon howLongOrShortCanASingleWordBe?
a b c d e f g f e d c b a
54254#@[email protected]^#^%^$hahaha#$@[email protected]
Hello buddy dance tango!
PleaseOhPleaseJumpOverMe
This is too much for me...

样例输出:

Scenario #1:
searching

Scenario #2:
-outside-
espanol

Scenario #3:
-outside-
hahaha

Scenario #4:
-too many-

解题代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
 public static void main(String[] args) throws NumberFormatException, IOException {
  BufferedReader re=new  BufferedReader(new InputStreamReader(System.in) );
  int T=Integer.parseInt(re.readLine());
  for(int i=1;i<=T;i++)
  {   int n=0,last=0;
    boolean[] use=new boolean[300010];
    Set strset=new TreeSet();
      for(int p=0;p< 3;p++)
      {
   String str=re.readLine().toLowerCase();
   str=str.replaceAll("[^a-zA-Z]+"," ").trim();//"[^a-zA-Z]+"匹配非字母字符串
   String[] sa=str.split(" ");
   for(int j=0;j< sa.length&&strset.size()<=3;j++,n++)
   {
    String te=sa[j];
    if(p==0||p==1&&use[n])
    {
     int next=n+te.length();
     use[next]=true;
     last=Math.max(last,next);
    }
    if(p==2&&use[n])
    {
     strset.add(te);
    }
   }
      }
      System.out.println("Scenario #"+i+":");
   if(last>=n)
    strset.add("-outside-");
   if(strset.size()>3)
    System.out.println("-too many-");
   else
   {
    Iterator it=strset.iterator();
    while(it.hasNext())
    
     System.out.println(it.next());
    
   }
   System.out.println();
      }
 
 }

}

  1. 第一句可以忽略不计了吧。从第二句开始分析,说明这个花色下的所有牌都会在其它里面出现,那么还剩下♠️和♦️。第三句,可以排除2和7,因为在两种花色里有。现在是第四句,因为♠️还剩下多个,只有是♦️B才能知道答案。

  2. 第一句可以忽略不计了吧。从第二句开始分析,说明这个花色下的所有牌都会在其它里面出现,那么还剩下♠️和♦️。第三句,可以排除2和7,因为在两种花色里有。现在是第四句,因为♠️还剩下多个,只有是♦️B才能知道答案。