题解 | #好串#

Keep In Line

https://ac.nowcoder.com/acm/problem/13822

爆掉了,应该写个map来记录是否在队列里面,这样查找起来更快,而不是while(true)。


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for(int i = 0;i<T;i++){
            int n = sc.nextInt();
            sc.nextLine();
            Queue<String> queue = new LinkedList<String>();
            int count = 0; 
            ArrayList<String> mistake = new ArrayList<String>();
            for(int j = 0;j<n;j++){
                String str = sc.nextLine();
                String []strArray = str.split(" ");
                if(strArray[0].equals("in")){
                    queue.offer(strArray[1]);
                }else{
                    for(int h = 0;h<mistake.size()+2;h++){
                    if(queue.peek().equals(strArray[1])){
                        // System.out.println(queue.peek());
                        count++;
                        queue.poll();
                        break;
                    }else{
                        boolean b = false;
                        for(int z = 0;z<mistake.size();z++){
                            if(mistake.get(z).equals(queue.peek())){
                                mistake.remove(z);
                                b = true;
                                break;
                            }
                        }
                        if(b){
                            queue.poll();
                        }else{
                            mistake.add(strArray[1]);
                            // System.out.println("********");
                            break;
                        }
                    }
                }
            }
        }
            System.out.println(count);
            
        }
    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务