首页 > 专题系列 > Java解POJ > POJ 3458 Colour Sequence [解题报告] Java
2013
11-12

POJ 3458 Colour Sequence [解题报告] Java

Colour Sequence

问题描述 :

We have a pile of cards consisting of 100 cards that are coloured on both sides. There is a finite number of colours (at most 26). In addition there are special cards called jokers. Jokers have a joker sign on both sides, which can assume any of the possible colours. We consider here a one-player card game, in which the player is challenged to derive a given colour sequence from a given row of cards, following certain rules.

Before the actual beginning of the game a colour sequence S of length at most 100 (not containing a joker) is given. Furthermore a number of cards are chosen from the pile and are put in a row. The sides turned upwards form a row of colours. Now the aim for the player is to create the colour sequence S with the cards from the row in the following way. For each card in the row the player decides whether or not to turn it over. When the card is turned over, only the colour on the other side is visible. Jokers may be part of the row of cards.

These steps lead to the final sequence of colours formed by the visible side of the cards in the row. If the player has been able to turn the cards in such a way that the pre-given colour sequence S is contained (from left to right) in the final row of colours, the player wins. If not, he loses. In matching the pre-given colour sequence to the row, cards in the row may be skipped, as long as the relative order of the colours is preserved. A joker can assume any colour. For example, the colour sequence (red, blue, yellow) is contained in (green, joker, blue, red, yellow), and (blue, green, blue, green) is contained in (red, blue, joker, yellow, joker, blue, green, green).

Your job is to find out if the player can win, given the colour sequence S and the row of cards chosen from the pile. This means that the sequence of colours that are face up is known, and so are the colours on the other side of these cards.

输入:

The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:

  • One line describing the colour sequence S. This line contains a string of m (with 1 ≤ m ≤ 100) characters from the set {'A', 'B', …, 'Z'}, denoting the colours. Different colours correspond to different characters. For example: "BGBG" denotes the sequence blue, green, blue, green.

  • Two lines, corresponding to the row of cards chosen from the pile. Each of these lines contains a string of k (1 ≤ k ≤ 100) characters from the set {'A', 'B', …, 'Z', '*'}. The character '*' denotes a joker, which can play the role of any of the possible colours.

    The string in the first line corresponds to the row of colours on the visible side of the cards. The string in the second line corresponds to the row of colours on the hidden side of the cards.

    So for the ith card in the row, the first line gives the colour of the side turned upwards and the second line shows the colour of the side face down. Obviously the strings on both lines have the same length. Furthermore, a '*' in one line (denoting a joker) always corresponds to a '*' in the other line at the corresponding position.

输出:

For every test case in the input file, the output should contain one line. This line contains "win" if the colour sequence S can be achieved by the player by turning the right cards upside down, and "lose" if this is not the case.

样例输入:

3
RBY
B*RRB
G*BRY
BGBG
RZ*Y*PGG
AB*Y*BCB
BAPC
BUBCDAPVDAVVDLPF
VLDCUSPGLSGPPVDD

样例输出:

win
win
lose

解题代码:

//* @author:
import java.util.*;
public class Main{
   public static void main(String args[]){
     Scanner in=new Scanner(System.in);
      int n=in.nextInt();
      int j=-1;
       while(n!=0){
          String test=in.next();
          String front=in.next();
          String back=in.next();
           int len1=test.length();
           int len2=front.length();
          for(int i=0;i< len2;i++){
            if((front.charAt(i)=='*')||(test.charAt(j+1)==front.charAt(i)) || (test.charAt(j+1)==back.charAt(i)))
               j++;
            if(j==len1-1)
               break;
          }
          if(j==len1-1)
			System.out.printf("win\n");
		else
			System.out.printf("lose\n");
		n--;
		j=-1;
	}
   }
}

  1. 。。。你有必要这么二吗,“都啥年代了”意思是“这又不是过去”,这你真的不知道? 老外里不但有毛粉,还有人爱吃翔呢,你跟老外一起路过厕所,还要先抬头看看老外有没有捂鼻子,然后你才敢捂鼻子?傻

  2. simple, however efficient. A lot of instances it is difficult to get that a??perfect balancea?? among usability and appearance. I must say that youa??ve done a exceptional task with this. Also, the blog masses quite fast for me on Web explore.

  3. 这道题目的核心一句话是:取还是不取。
    如果当前取,则index+1作为参数。如果当前不取,则任用index作为参数。

  4. 一开始就规定不相邻节点颜色相同,可能得不到最优解。我想个类似的算法,也不确定是否总能得到最优解:先着一个点,随机挑一个相邻点,着第二色,继续随机选一个点,但必须至少有一个边和已着点相邻,着上不同色,当然尽量不增加新色,直到完成。我还找不到反例验证他的错误。。希望LZ也帮想想, 有想法欢迎来邮件。谢谢