tookunn’s diary

主に競技プログラミング関係

RUPC2016 Day2 C ABNN is 17 years old forever

問題

http://judge.u-aizu.ac.jp/onlinejudge/cdescription.jsp?cid=RitsCamp16Day2&pid=C

当時は結構苦戦してたけど,今やるとUnionFindの練習ぐらいに感じる...

考察

村の合併をUnionFindで表現して,合併するごとにrankを増加させていき,最終的にrankが1の時は村とし,それ以外だったら町とする。

ソースコード

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.NoSuchElementException;

public class Main{
	int N,M;
	int[] union,rank;
	boolean[] used;

	public boolean same(int x,int y){
		return find(x) == find(y);
	}

	public int find(int x){
		if(union[x] == x)return x;
		return union[x] = find(union[x]);
	}

	public void unite(int x,int y){
		x = find(x);
		y = find(y);

		if(x == y)return;

		if(x < y){
			union[y] = x;
			rank[x] += rank[y];
		}else{
			union[x] = y;
			rank[y] += rank[x];
		}
	}

	public void solve() {
		N = nextInt();
		M = nextInt();

		union = new int[N];
		rank = new int[N];
		used = new boolean[N];
		for(int i = 0;i < N;i++)union[i] = i;
		Arrays.fill(rank, 1);


		for(int i = 0;i < M;i++){
			int a = nextInt()-1;
			int b = nextInt()-1;

			unite(a,b);
		}

		int big = 0;
		int small = 0;
		for(int i = 0;i < N;i++){
			int root = find(i);
			if(used[root])continue;
			used[root] = true;

			if(rank[root] == 1)small++;
			else big++;
		}

		out.println(Math.abs(big-small));
	}

	public static void main(String[] args) {
		out.flush();
		new Main().solve();
		out.close();
	}

	/* Input */
	private static final InputStream in = System.in;
	private static final PrintWriter out = new PrintWriter(System.out);
	private final byte[] buffer = new byte[2048];
	private int p = 0;
	private int buflen = 0;

	private boolean hasNextByte() {
		if (p < buflen)
			return true;
		p = 0;
		try {
			buflen = in.read(buffer);
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (buflen <= 0)
			return false;
		return true;
	}

	public boolean hasNext() {
		while (hasNextByte() && !isPrint(buffer[p])) {
			p++;
		}
		return hasNextByte();
	}

	private boolean isPrint(int ch) {
		if (ch >= '!' && ch <= '~')
			return true;
		return false;
	}

	private int nextByte() {
		if (!hasNextByte())
			return -1;
		return buffer[p++];
	}

	public String next() {
		if (!hasNext())
			throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = -1;
		while (isPrint((b = nextByte()))) {
			sb.appendCodePoint(b);
		}
		return sb.toString();
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

	public long nextLong() {
		return Long.parseLong(next());
	}

	public double nextDouble() {
		return Double.parseDouble(next());
	}
}

RUPC2016 Day2 B SEARIGHT LIVE FES

考察

外側の角の部分から上,横,下に分けて考える。

外側の人がa番目に存在する
内側の人がb番目に存在する

以上を前提とすると

  • 外側の人が上(W-1番目まで)に存在する場合,a == bで判定
  • 外側の人が横(W+1番目からW+H-2番目まで)に存在する場合,a-2 == bで判定
  • 外側の人が下(W+H-2+1番目からW*2+H-2番目まで)に存在する場合,a-4 == bで判定

ソースコード

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.NoSuchElementException;

public class Main{
	int W,H,N,a,b;
	public void solve() {
		W = nextInt();
		H = nextInt();
		N = nextInt();

		int ans = 0;
		a = 1;
		b = 1;
		for(int i = 0;i < N;i++){
			int p = nextInt();
			if(p==0)a++;
			else b++;

			if(a <= W-1){
				if(a == b)ans++;
			}else if(a == W){
				continue;
			}else if(W + 1 <= a && a <= W + H - 2){
				if(a - 2 == b)ans++;
			}else if(a == W + H - 2 + 1){
				continue;
			}else if(a >= W + H){
				if(a - 4 == b)ans++;
			}
		}
		out.println(ans);
	}

	public static void main(String[] args) {
		out.flush();
		new Main().solve();
		out.close();
	}

	/* Input */
	private static final InputStream in = System.in;
	private static final PrintWriter out = new PrintWriter(System.out);
	private final byte[] buffer = new byte[2048];
	private int p = 0;
	private int buflen = 0;

	private boolean hasNextByte() {
		if (p < buflen)
			return true;
		p = 0;
		try {
			buflen = in.read(buffer);
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (buflen <= 0)
			return false;
		return true;
	}

	public boolean hasNext() {
		while (hasNextByte() && !isPrint(buffer[p])) {
			p++;
		}
		return hasNextByte();
	}

	private boolean isPrint(int ch) {
		if (ch >= '!' && ch <= '~')
			return true;
		return false;
	}

	private int nextByte() {
		if (!hasNextByte())
			return -1;
		return buffer[p++];
	}

	public String next() {
		if (!hasNext())
			throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = -1;
		while (isPrint((b = nextByte()))) {
			sb.appendCodePoint(b);
		}
		return sb.toString();
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

	public long nextLong() {
		return Long.parseLong(next());
	}

	public double nextDouble() {
		return Double.parseDouble(next());
	}
}

RUPC2016 Day2 A Gossip

考察

max(a[i]-a[i-1])/2,a[0]-1,N-a[M-1])が答え。
つまり最初に情報を持っているアイドルの間隔を2で割った値の最大値が解だが,アイドル1とアイドルNがそれぞれ情報持っているアイドルの中で一番間隔が近いアイドルとの間隔も先程の解の候補に含める。

ソースコード

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.NoSuchElementException;

public class Main{
	int N,M;
	int[] a;
	public void solve() {
		N = nextInt();
		M = nextInt();
		a = new int[M];
		for(int i = 0;i < M;i++)a[i] = nextInt();
		int ans = 0;
		for(int i = 0;i <= M;i++){
			if(i == 0)ans = Math.max(ans, a[i]-1);
			else if(i==M)ans = Math.max(ans,N-a[i-1]);
			else ans = Math.max(ans, (a[i]-a[i-1])/2);
		}
		out.println(ans);
	}

	public static void main(String[] args) {
		out.flush();
		new Main().solve();
		out.close();
	}

	/* Input */
	private static final InputStream in = System.in;
	private static final PrintWriter out = new PrintWriter(System.out);
	private final byte[] buffer = new byte[2048];
	private int p = 0;
	private int buflen = 0;

	private boolean hasNextByte() {
		if (p < buflen)
			return true;
		p = 0;
		try {
			buflen = in.read(buffer);
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (buflen <= 0)
			return false;
		return true;
	}

	public boolean hasNext() {
		while (hasNextByte() && !isPrint(buffer[p])) {
			p++;
		}
		return hasNextByte();
	}

	private boolean isPrint(int ch) {
		if (ch >= '!' && ch <= '~')
			return true;
		return false;
	}

	private int nextByte() {
		if (!hasNextByte())
			return -1;
		return buffer[p++];
	}

	public String next() {
		if (!hasNext())
			throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = -1;
		while (isPrint((b = nextByte()))) {
			sb.appendCodePoint(b);
		}
		return sb.toString();
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

	public long nextLong() {
		return Long.parseLong(next());
	}

	public double nextDouble() {
		return Double.parseDouble(next());
	}
}

RUPC2016 Day1 F リレー / Relay

考察

基本的には公式解説にある通りの考え方で何となく理解しましたが,オイラーツアーを行う実装がイメージできなかったので,mayokoさんの解説記事のコードを参考にさせていただきました。セグメントツリーは蟻本写経しました。

公式解説よりうまく説明できないと思うので,メモ程度に書いていきます。


まず,求めたいのが木の直径を構成する端点 u,v なので,それらの端点をdouble sweepというアルゴリズムを使って求める。

double sweep

  • 木から適当に頂点を決めてそこから最も距離が遠い頂点を v
  • vから最も距離が遠い頂点を u
  • v と u はその木の直径を構成する頂点となる。つまりv と u の距離がその木の直径となる。


その後 u,v を根としてそれぞれからオイラーツアーを行う。
(u,v)から付け替える辺 e を除いて到達できる最も遠い頂点を決め距離を求める。


オイラーツアーでやること

  • depth[v] = 根から頂点vまでの距離
  • first[v] = 根から深さ優先探索をして最初に頂点vに到達した時点のそれまでの頂点を訪れた回数
  • last[v] = 根から深さ優先探索をして最後に頂点vに到達した時点のそれまでの頂点を訪れた回数
  • 初めて訪れた頂点でfirst[v]に訪れた回数を記録,depth[v]に根からの距離を記録
  • その頂点から出ていき,もう訪れない時はlast[v]に訪れた回数を記録する


公式解説:
立命合宿 2016 - 立命館大学情報理工学部プロジェクト団体プログラミングコンテスト部門 RiPPro

参考にした記事等:
mayokoex.hatenablog.com

あとは公式解説内にあるリンク先とか参考にしました。

ソースコード

恐らく僕の実装があまり良くないらしくてスタックのサイズを指定しないとRuntime Errorになってしまいます。

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.NoSuchElementException;

public class Main implements Runnable{
	int N,num,size;
	int[] depth,first,last,segment;
	ArrayList<Edge>[] graph;

	private class Edge{
		int to,weight;
		public Edge(int to,int weight){
			this.to = to;
			this.weight = weight;
		}
	}

	public void init(int n){
		size = 1;
		while(size < n)size<<=1;
		segment = new int[2*size];
		for(int i = 0;i < 2 * size - 1;i++)segment[i]=Integer.MIN_VALUE;
	}
	public void update(int k,int a){
		k += size - 1;
		segment[k] = a;
		while(k > 0){
			k = (k - 1) / 2;
			segment[k] = Math.max(segment[k * 2 + 1], segment[k * 2 + 2]);
		}
	}
	public int query(int a,int b,int k,int l,int r){
		if(r <= a || b <= l)return Integer.MIN_VALUE;
		if(a <= l && r <= b)return segment[k];
		int vl = query(a,b,k * 2 + 1,l,(l + r)/2);
		int vr = query(a,b,k * 2 + 2,(l + r)/2,r);
		return Math.max(vl, vr);
	}

	public int[] dfs(int prev,int now){
		int[] ret = {now,0};
		for(Edge edge : graph[now]){
			if(edge.to != prev){
				int[] tmp = dfs(now,edge.to);
				tmp[1]+=edge.weight;
				if(ret[1] < tmp[1])ret = tmp;
			}
		}
		return ret;
	}

	//木の直径の端点u,vを求める
	public int[] doubleSweep(){
		int u = dfs(-1,0)[0];
		int v = dfs(-1,u)[0];
		return new int[]{u,v};
	}

	public void eulerTour(int prev,int now,int dis){
		depth[now] = dis;
		first[now] = num++;
		for(Edge edge : graph[now]){
			if(edge.to != prev){
				eulerTour(now,edge.to,dis+edge.weight);
				num++;
			}
		}
		last[now] = num;
	}

	@SuppressWarnings("unchecked")
	public void solve() {
		N = nextInt();

		graph = new ArrayList[N];
		for(int i = 0;i < N;i++)graph[i] = new ArrayList<Edge>();

		for(int i = 0;i < N-1;i++){
			int p = nextInt();
			int w = nextInt();
			graph[i+1].add(new Edge(p,w));
			graph[p].add(new Edge(i+1,w));
		}

		int[] tmp = doubleSweep();
		int u = tmp[0];
		int v = tmp[1];
		int ans = 0;
		int M = 2 * N;

		depth = new int[M];
		first = new int[M];
		last = new int[M];

		//u
		num = 0;
		eulerTour(-1,u,0);

		init(2*M);
		for(int i = 0;i < N;i++){
			update(first[i],depth[i]);
		}
		for(int i = 0;i < N;i++){
			for(Edge edge : graph[i]){
				int to = edge.to;
				if(depth[edge.to] < depth[i])to = i;
				int vl = query(0,first[to],0,0,size);
				int vr = query(last[to],M-1,0,0,size);
				ans = Math.max(ans,Math.max(vl, vr)+edge.weight);
			}
		}

		Arrays.fill(depth, 0);
		Arrays.fill(first,0);
		Arrays.fill(last, 0);
		Arrays.fill(segment, Integer.MIN_VALUE);
		num = 0;

		//v
		eulerTour(-1,v,0);

		for(int i = 0;i < N;i++){
			update(first[i],depth[i]);
		}
		for(int i = 0;i < N;i++){
			for(Edge edge : graph[i]){
				int to = edge.to;
				if(depth[edge.to] < depth[i])to = i;
				int vl = query(0,first[to],0,0,size);
				int vr = query(last[to],M-1,0,0,size);
				ans = Math.max(ans,Math.max(vl, vr)+edge.weight);
			}
		}

		out.println(ans);
	}

	public static void main(String[] args) {
		new Thread(null,new Main(),"",32*1024 * 1024).start();
	}

	/* Input */
	private static final InputStream in = System.in;
	private static final PrintWriter out = new PrintWriter(System.out);
	private final byte[] buffer = new byte[2048];
	private int p = 0;
	private int buflen = 0;

	private boolean hasNextByte() {
		if (p < buflen)
			return true;
		p = 0;
		try {
			buflen = in.read(buffer);
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (buflen <= 0)
			return false;
		return true;
	}

	public boolean hasNext() {
		while (hasNextByte() && !isPrint(buffer[p])) {
			p++;
		}
		return hasNextByte();
	}

	private boolean isPrint(int ch) {
		if (ch >= '!' && ch <= '~')
			return true;
		return false;
	}

	private int nextByte() {
		if (!hasNextByte())
			return -1;
		return buffer[p++];
	}

	public String next() {
		if (!hasNext())
			throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = -1;
		while (isPrint((b = nextByte()))) {
			sb.appendCodePoint(b);
		}
		return sb.toString();
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

	public long nextLong() {
		return Long.parseLong(next());
	}

	public double nextDouble() {
		return Double.parseDouble(next());
	}
	@Override
	public void run() {
		out.flush();
		new Main().solve();
		out.close();
	}
}

RUPC2016 Day1 E 28

考察

良い整数は各桁に,2または8しか現れないので10^{18}までの正整数はそこまで多くない。
なので、考えられる良い整数はすべて生成しておき、nを割り切ることが出来る良い整数だけを抜き出しておく。

抜き出した良い整数の積からNが構成できるかどうかをメモ化全探索する。

ソースコード

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.NoSuchElementException;

public class Main {
	long N;
	ArrayList<Long> all,sub;
	HashMap<Long,Integer>[] map;
	public void createGoodNumber(){
		all = new ArrayList<Long>();
		ArrayDeque<Long> q = new ArrayDeque<Long>();
		q.add(2L);
		q.add(8L);
		while(q.size() > 0){
			long L = q.poll();
			all.add(L);
			if(L >= (long)1e17)continue;
			q.add(L * 10 + 2);
			q.add(L * 10 + 8);
		}
		Collections.sort(all);
	}

	public int dfs(int i,long n){
		if(n == 1){
			return 0;
		}

		if(map[i].containsKey(n)){
			return map[i].get(n);
		}

		int ret = -1;

		for(int j = i;j < sub.size();j++){
			if(n % sub.get(j) == 0){
				int tmp = dfs(j,n / sub.get(j));
				if(tmp+1 > 0){
					map[i].put(n, tmp+1);
					return tmp+1;
				}
			}
		}

		map[i].put(n,ret);
		return ret;
	}


	@SuppressWarnings("unchecked")
	public void solve() {
		N = nextLong();
		createGoodNumber();
		sub = new ArrayList<Long>();

		for(int i = 0;i < all.size();i++){
			if(N % all.get(i) == 0)sub.add(all.get(i));
		}

		if(N == 1){
			out.println(-1);
			return;
		}

		map = new HashMap[sub.size()];
		for(int i = 0;i < sub.size();i++){
			map[i] = new HashMap<Long,Integer>();
		}

		if(sub.size() == 0){
			out.println(-1);
		}else{
			out.println(dfs(0,N));
		}
	}

	public static void main(String[] args) {
		out.flush();
		new Main().solve();
		out.close();
	}

	/* Input */
	private static final InputStream in = System.in;
	private static final PrintWriter out = new PrintWriter(System.out);
	private final byte[] buffer = new byte[2048];
	private int p = 0;
	private int buflen = 0;

	private boolean hasNextByte() {
		if (p < buflen)
			return true;
		p = 0;
		try {
			buflen = in.read(buffer);
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (buflen <= 0)
			return false;
		return true;
	}

	public boolean hasNext() {
		while (hasNextByte() && !isPrint(buffer[p])) {
			p++;
		}
		return hasNextByte();
	}

	private boolean isPrint(int ch) {
		if (ch >= '!' && ch <= '~')
			return true;
		return false;
	}

	private int nextByte() {
		if (!hasNextByte())
			return -1;
		return buffer[p++];
	}

	public String next() {
		if (!hasNext())
			throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = -1;
		while (isPrint((b = nextByte()))) {
			sb.appendCodePoint(b);
		}
		return sb.toString();
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

	public long nextLong() {
		return Long.parseLong(next());
	}

	public double nextDouble() {
		return Double.parseDouble(next());
	}
}

RUPC2016 Day1 D スキャナー / Scanner

考察

当初、貪欲に時間のかかる紙から選んで、早くスキャンが終わっているスキャナーに振り分けた。が、通らなかった。

DP[i番目の紙][1つ目のスキャナーのスキャン終了時間][2つ目のスキャナーのスキャン終了時間] = このケースが存在するか(true または false)

3つ目のスキャナーのスキャン終了時間は、残り2つのスキャナーのスキャン終了時間が分かれば分かる。(この考察は解説見る前に気付いたが、ここからどうすればよいのか分からなかった。(メモ))

JavaDP[N+1][2500][2500]でギリギリ1.88secぐらいだったので以下記事のように2500 \to 900にした方が良い。

参考にさせていただいた解説記事:
pakapa104.hatenablog.com

ソースコード

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.NoSuchElementException;

public class Main {
	int N;
	int[] T;
	boolean[][][] dp;

	public void solve() {
		N = nextInt();
		T = new int[N];
		int sum = 0;
		for(int i = 0;i < N;i++){
			T[i] = nextInt();
			sum += T[i];
		}

		dp = new boolean[N + 1][2500 + 1][2500 + 1];
		dp[0][0][0] = true;

		for(int i = 0;i < N;i++){

			for(int j = 0;j < 2500 + 1;j++){

				for(int k = 0;k < 2500 + 1;k++){

					if(!dp[i][j][k])continue;

					if(j + T[i] < 2500)dp[i + 1][j + T[i]][k] = true;

					if(k + T[i] < 2500)dp[i + 1][j][k + T[i]] = true;

					dp[i + 1][j][k] = true;

				}

			}

		}

		int ans = Integer.MAX_VALUE;

		for(int i = 0;i < 2500 + 1;i++){
			for(int j = 0;j < 2500 + 1;j++){
				if(dp[N][i][j]){
					ans = Math.min(ans, Math.max(sum - i - j,Math.max(i, j)));
				}

			}
		}

		out.println(ans);
	}
	public static void main(String[] args) {
		out.flush();
		new Main().solve();
		out.close();
	}

	/* Input */
	private static final InputStream in = System.in;
	private static final PrintWriter out = new PrintWriter(System.out);
	private final byte[] buffer = new byte[2048];
	private int p = 0;
	private int buflen = 0;

	private boolean hasNextByte() {
		if (p < buflen)
			return true;
		p = 0;
		try {
			buflen = in.read(buffer);
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (buflen <= 0)
			return false;
		return true;
	}

	public boolean hasNext() {
		while (hasNextByte() && !isPrint(buffer[p])) {
			p++;
		}
		return hasNextByte();
	}

	private boolean isPrint(int ch) {
		if (ch >= '!' && ch <= '~')
			return true;
		return false;
	}

	private int nextByte() {
		if (!hasNextByte())
			return -1;
		return buffer[p++];
	}

	public String next() {
		if (!hasNext())
			throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = -1;
		while (isPrint((b = nextByte()))) {
			sb.appendCodePoint(b);
		}
		return sb.toString();
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

	public long nextLong() {
		return Long.parseLong(next());
	}

	public double nextDouble() {
		return Double.parseDouble(next());
	}
}

RUPC2016 Day1 C 足し算掛け算 / AddMul

考察

出現するアルファベットの数を、1~9までの出来るだけ大きい数を括弧でくくれるようにカウントする。

C_iを文字列Sの中にi回出現するアルファベットの数とする。
もし,C_1が1以上だったら(C_1-1)*2+1ansに加える、それ以外だったら何も加えない。

そして、iを2~9まで見ていき、その中で以下の処理を行う

  • C_i2以上で,ans=0 なら(C_i-1)*2+1+4,ans \ne 0なら(C_i-1)*2+4+1+1ansに加える。
  • C_i1で,ans=0なら3,ans \ne 0なら4をansに加える。

ソースコード

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.NoSuchElementException;

public class Main {
	int N;
	String S;
	int[] a;
	@SuppressWarnings("unchecked")
	public void solve() {
		N = nextInt();
		S = next();
		a = new int[26];

		for(int i = 0;i < N;i++){
			if(S.charAt(i) >= 'a' && S.charAt(i) <= 'z')
				a[S.charAt(i)-'a']++;
		}

		ArrayList<Integer>[] list = new ArrayList[10];
		for(int i = 0;i < 10;i++){
			list[i] = new ArrayList<Integer>();
		}

		for(int i = 0;i < 26;i++){
			if(a[i] == 0)continue;
			for(int j = 9;j >= 1;j--){
				if(a[i]%j==0){
					list[j].add(i);
					break;
				}
			}
		}

		int ans = list[1].size() > 0 ? (list[1].size() - 1)* 2 + 1 : 0;

		for(int i = 2;i < 10;i++){

			if(list[i].size() >= 2){
				if(ans == 0)ans += (list[i].size() -1) * 2 + 1 + 4;
				else ans += (list[i].size() -1) * 2 + 4 + 1 + 1;
			}else if(list[i].size() == 1){
				if(ans == 0)ans += 3;
				else ans += 4;
			}
		}

		out.println(ans);
	}
	public static void main(String[] args) {
		out.flush();
		new Main().solve();
		out.close();
	}

	/* Input */
	private static final InputStream in = System.in;
	private static final PrintWriter out = new PrintWriter(System.out);
	private final byte[] buffer = new byte[2048];
	private int p = 0;
	private int buflen = 0;

	private boolean hasNextByte() {
		if (p < buflen)
			return true;
		p = 0;
		try {
			buflen = in.read(buffer);
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (buflen <= 0)
			return false;
		return true;
	}

	public boolean hasNext() {
		while (hasNextByte() && !isPrint(buffer[p])) {
			p++;
		}
		return hasNextByte();
	}

	private boolean isPrint(int ch) {
		if (ch >= '!' && ch <= '~')
			return true;
		return false;
	}

	private int nextByte() {
		if (!hasNextByte())
			return -1;
		return buffer[p++];
	}

	public String next() {
		if (!hasNext())
			throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = -1;
		while (isPrint((b = nextByte()))) {
			sb.appendCodePoint(b);
		}
		return sb.toString();
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

	public long nextLong() {
		return Long.parseLong(next());
	}

	public double nextDouble() {
		return Double.parseDouble(next());
	}
}