[JAVA][HDU 1022][Train Problem I]

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. import java.io.BufferedInputStream;  
  2. import java.util.Scanner;  
  3. import java.util.Stack;  
  4.   
  5. public class Main {  
  6.   
  7.     public static void main(String[] args) {  
  8.         Scanner sc = new Scanner(new BufferedInputStream(System.in));  
  9.         Stack<Character> stack = new Stack<Character>();  
  10.         while (sc.hasNext()) {  
  11.             stack.empty();  
  12.             int n = sc.nextInt();  
  13.             String inSeq = sc.next();  
  14.             String outSeq = sc.next();  
  15.             char opSeq[] = new char[inSeq.length() * 2 + 1];// record the operates of the exchanging  
  16.             int inPos = 0;  
  17.             int outPos = 0;  
  18.             int seqPos = 0;  
  19.             while (outPos < n) {  
  20.                 if (!stack.isEmpty() && stack.peek() == outSeq.charAt(outPos)) {  
  21.                     outPos++;  
  22.                     // if the stack is not empty and the top of the stack fit the out seq  
  23.                     stack.pop();  
  24.                     opSeq[seqPos++] = 'o';  
  25.                 } else if (inPos < n) {  
  26.                     stack.push(inSeq.charAt(inPos++));  
  27.                     opSeq[seqPos++] = 'i';  
  28.                 } else {// if we can push or pop,then we cant finish the exchanging  
  29.                     break;  
  30.                 }  
  31.             }  
  32.             if (stack.isEmpty()) {// if stack is empty means that the exchanging is success  
  33.                 System.out.println("Yes.");  
  34.                 for (int i = 0; i < inSeq.length() * 2; i++) {  
  35.                     if (opSeq[i] == 'i') {  
  36.                         System.out.println("in");  
  37.                     } else {  
  38.                         System.out.println("out");  
  39.                     }  
  40.                 }  
  41.   
  42.             } else {  
  43.                 System.out.println("No.");  
  44.             }  
  45.             System.out.println("FINISH");  
  46.             stack.clear();  
  47.         }  
  48.     }  
  49. }  


C还需要自己写个栈,JAVA就简单多了,一开始没有clearstack出了一次WA,其实还是挺简单的。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值