POJ1219 L-I-N-G-O: LINGO

来源:互联网 发布:设ab都是n阶对称矩阵 编辑:程序博客网 时间:2024/04/30 03:33

题目:http://acm.pku.edu.cn/JudgeOnline/problem?id=1219

思路:题目不难,但千万读清楚题,输入输出也有陷阱。。。

 

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. public class Main {
  5.     public static void main(String[] args) throws IOException {
  6.         BufferedReader read = new BufferedReader(new InputStreamReader(
  7.                 System.in));
  8.         String s;
  9.         String r;
  10.         int time;
  11.         char[] last;
  12.         StringBuilder temp;
  13.         char[] buff;
  14.         int pos;
  15.         boolean[] used;
  16.         game: while (!(s = read.readLine()).equals("LINGO")) {
  17.             if (s.equals("") || s == null) {
  18.                 continue;
  19.             }
  20.             System.out.println();
  21.             r = s;
  22.             last = new char[] { '.''.''.''.''.' };
  23.             last[0] = r.charAt(0);
  24.             System.out.println(last);
  25.             time = 1;
  26.             turn: while (!(s = read.readLine()).equals("")) {
  27.                 // check valid
  28.                 if (s.length() != 5) {
  29.                     System.out.println(last);
  30.                     time++;
  31.                     continue turn;
  32.                 }
  33.                 for (int i = 0; i < 5; i++) {
  34.                     if (!Character.isUpperCase(s.charAt(i))) {
  35.                         System.out.println(last);
  36.                         time++;
  37.                         continue turn;
  38.                     }
  39.                 }
  40.                 // is right
  41.                 if (s.equals(r)) {
  42.                     System.out.println(r);
  43.                     over(read);
  44.                     continue game;
  45.                 }
  46.                 if (time == 6) {
  47.                     System.out.println(r.toLowerCase());
  48.                     over(read);
  49.                     continue game;
  50.                 }
  51.                 // check
  52.                 buff = new char[] { '.''.''.''.''.' };
  53.                 temp = new StringBuilder(r);
  54.                 used = new boolean[5];
  55.                 for (int i = 0; i < 5; i++) {
  56.                     if (r.charAt(i) == s.charAt(i)) {
  57.                         buff[i] = s.charAt(i);
  58.                         temp.setCharAt(i, ' ');
  59.                         used[i] = true;
  60.                     }
  61.                 }
  62.                 for (int i = 0; i < 5; i++) {
  63.                     if (used[i]) {
  64.                         continue;
  65.                     }
  66.                     if ((pos = temp.indexOf(s.charAt(i) + "")) != -1) {
  67.                         buff[i] = Character.toLowerCase(s.charAt(i));
  68.                         temp.setCharAt(pos, ' ');
  69.                         used[i] = true;
  70.                     }
  71.                 }
  72.                 System.out.println(buff);
  73.                 last = buff;
  74.                 time++;
  75.             }
  76.             if (time < 7) {
  77.                 System.out.println(r.toLowerCase());
  78.             }
  79.         }
  80.     }
  81.     public static void over(BufferedReader read) throws IOException {
  82.         while (!read.readLine().equals("")) {
  83.         }
  84.     }
  85. }