codeforces 58A(Chat room) Java

日常水题,刷刷!!!

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;

/**
 * 题意:输入字符串,删除其中一些字符,如果字符可以变为 hello ,则输出 YES , 否则输出 NO
 *
 * @author TinyDolphin
 *         2017/6/24 14:25.
 */
public class Main {

    /**
     * 判断数组是否有序,且相邻元素不相等(arr[n] > arr[n-1])
     *
     * @param arr 待判断数组
     * @return 若数组有序,返回 true,否则返回 false
     */
    public static boolean isSort(int[] arr) {
        for (int index = 1; index < arr.length; index++) {
            if (arr[index] <= arr[index - 1]) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) throws IOException {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        String inputWord;
        int[] location = new int[5];
        while (in.nextToken() != StreamTokenizer.TT_EOF) {
            inputWord = in.sval;
            location[0] = inputWord.indexOf("h", 0);
            location[1] = inputWord.indexOf("e", location[0] + 1);
            location[2] = inputWord.indexOf("l", location[1] + 1);
            location[3] = inputWord.indexOf("l", location[2] + 1);
            location[4] = inputWord.indexOf("o", location[3] + 1);
            out.println(isSort(location) ? "YES" : "NO");
        }
        out.flush();
    }
}

做出以下优化,减少代码量。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;

/**
 * 题意:输入字符串,删除其中一些字符,如果字符可以变为 hello ,则输出 YES , 否则输出 NO
 *
 * @author TinyDolphin
 *         2017/5/27 14:25.
 */
public class Main {

    public static void main(String[] args) throws IOException {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        String inputWord;
        String hello = "hello ";//注意此处,最后需要多一个字符,如空格
        while (in.nextToken() != StreamTokenizer.TT_EOF) {
            inputWord = in.sval;
            int indexJ = 0;
            for (int indexI = 0; indexI < inputWord.length(); indexI++) {
                if (inputWord.charAt(indexI) == hello.charAt(indexJ)) {
                    indexJ++;
                }
            }
            out.println(indexJ == 5 ? "YES" : "NO");
        }
        out.flush();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值