import java.text.DecimalFormat;
import java.util.stream.LongStream;
import java.util.stream.IntStream;
import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) {
        FastScanner sc = new FastScanner();
        PrintWriter out = new PrintWriter(System.out);

        AtCoder problem = new AtCoder(sc);
        problem.solve(out);

        out.flush();
    }

}

class AtCoder {

    String S;

    AtCoder(FastScanner sc) {
        S = sc.next();
    }

    void solve(PrintWriter out) {
        StringBuilder sb = new StringBuilder(S).reverse();
        while (sb.length() > 0) {
            if (sb.substring(0, 5).equals("maerd")) sb.delete(0, 5);
            else if (sb.substring(0, 5).equals("esare")) sb.delete(0, 5);
            else if (sb.substring(0, 6).equals("resare")) sb.delete(0, 6);
            else if (sb.substring(0, 7).equals("remaerd")) sb.delete(0, 7);
            else {
                out.println("NO");
                return;
            }
        }
        out.println("YES");
    }

}

class FastScanner {

    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;

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

    private int readByte() {
        if (hasNextByte()) {
            return buffer[ptr++];
        } else {
            return -1;
        }
    }

    private static boolean isPrintableChar(int c) {
        return 33 <= c && c <= 126;
    }

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

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

    public long nextLong() {
        if (!hasNext()) {
            throw new NoSuchElementException();
        }
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while (true) {
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            } else if (b == -1 || !isPrintableChar(b)) {
                return minus ? -n : n;
            } else {
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }

    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) {
            throw new NumberFormatException();
        }
        return (int) nl;
    }

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

    public int[] arrayInt(int N) {
        int[] array = new int[N];
        for (int i = 0; i < N; i++) {
            array[i] = nextInt();
        }
        return array;
    }

    public long[] arrayLong(int N) {
        long[] array = new long[N];
        for (int i = 0; i < N; i++) {
            array[i] = nextLong();
        }
        return array;
    }

    public double[] arrayDouble(int N) {
        double[] array = new double[N];
        for (int i = 0; i < N; i++) {
            array[i] = nextDouble();
        }
        return array;
    }

    public String[] arrayString(int N) {
        String[] array = new String[N];
        for (int i = 0; i < N; i++) {
            array[i] = next();
        }
        return array;
    }
}

class My {

    static String sort(String s) {
        char[] ch = s.toCharArray();
        Arrays.sort(ch);
        return String.valueOf(ch);
    }

    static String reverse(String s) {
        return new StringBuilder(s).reverse().toString();
    }

    static int[] reverse(int[] array) {
        for (int i = 0; i < array.length / 2; i++) {
            int temp = array[i];
            array[i] = array[array.length - 1 - i];
            array[array.length - 1 - i] = temp;
        }
        return array;
    }

    static long[] reverse(long[] array) {
        for (int i = 0; i < array.length / 2; i++) {
            long temp = array[i];
            array[i] = array[array.length - 1 - i];
            array[array.length - 1 - i] = temp;
        }
        return array;
    }

    static double[] reverse(double[] array) {
        for (int i = 0; i < array.length / 2; i++) {
            double temp = array[i];
            array[i] = array[array.length - 1 - i];
            array[array.length - 1 - i] = temp;
        }
        return array;
    }

    static String[] reverse(String[] array) {
        for (int i = 0; i < array.length / 2; i++) {
            String temp = array[i];
            array[i] = array[array.length - 1 - i];
            array[array.length - 1 - i] = temp;
        }
        return array;
    }

    static char[] reverse(char[] array) {
        for (int i = 0; i < array.length / 2; i++) {
            char temp = array[i];
            array[i] = array[array.length - 1 - i];
            array[array.length - 1 - i] = temp;
        }
        return array;
    }

    static long min(long... numbers) {
        Arrays.sort(numbers);
        return numbers[0];
    }

    static int min(int... numbers) {
        Arrays.sort(numbers);
        return numbers[0];
    }

    static double min(double... numbers) {
        Arrays.sort(numbers);
        return numbers[0];
    }

    static long max(long... numbers) {
        Arrays.sort(numbers);
        return numbers[numbers.length - 1];
    }

    static int max(int... numbers) {
        Arrays.sort(numbers);
        return numbers[numbers.length - 1];
    }

    static double max(double... numbers) {
        Arrays.sort(numbers);
        return numbers[numbers.length - 1];
    }

    static int sum(long number) {
        int sum = 0;
        while (number > 0) {
            sum += number % 10;
            number /= 10;
        }
        return sum;
    }

    static Map<Integer, Integer> tally(int[] array) {
        Map<Integer, Integer> map = new TreeMap<>();
        for (int k : array) {
            int v = map.getOrDefault(k, 0);
            map.put(k, ++v);
        }
        return map;
    }

    static Map<Long, Integer> tally(long[] array) {
        Map<Long, Integer> map = new TreeMap<>();
        for (long k : array) {
            int v = map.getOrDefault(k, 0);
            map.put(k, ++v);
        }
        return map;
    }

    static Map<Double, Integer> tally(double[] array) {
        Map<Double, Integer> map = new TreeMap<>();
        for (double k : array) {
            int v = map.getOrDefault(k, 0);
            map.put(k, ++v);
        }
        return map;
    }

    static Map<String, Integer> tally(String[] array) {
        Map<String, Integer> map = new TreeMap<>();
        for (String k : array) {
            int v = map.getOrDefault(k, 0);
            map.put(k, ++v);
        }
        return map;
    }

    static Map<Character, Integer> tally(char[] array) {
        Map<Character, Integer> map = new TreeMap<>();
        for (char k : array) {
            int v = map.getOrDefault(k, 0);
            map.put(k, ++v);
        }
        return map;
    }

}


import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);

		/*<考察>
		 * dream を a
		 * dreamer を b
		 * erase を c
		 * eraser を d とおく。
		 *
		 * s と a,b,c,dを何度も比較する 4 *10^5で十分間に合う
		 * 比較するときに文字列がずれるためできないのでは。。。？
		 * 前から比較するとdream,dreamerのどっちで切っていいか分からなくなる、このような関係をprefixという。
		 *
		 * dream,dreamer,erase,eraser以外の異物が入っているときに絶対不可能であるため、
		 * 変な文字が入ってないか確認するか？
		 *
		*/

		/*String s = stdIn.next();

		if(!(s.contains("dream") || s.contains("dreamer") || s.contains("erase") || s.contains("eraser")) ) {
			System.out.println("NO");
		}else {
			System.out.println("YES");
		}
		*/

//		String[] divide = new String[] {"dream","dreamer","erase","eraser"};
		StringBuilder[] sb = new StringBuilder[] {new StringBuilder("dream"),new StringBuilder("dreamer"),new StringBuilder("erase"),new StringBuilder("eraser")};

		for(int i = 0; i < sb.length; i++) {
			sb[i] = sb[i].reverse();
		}

		String s2 = stdIn.next();

		StringBuilder s1 = new StringBuilder(s2);
		String s = s1.reverse().toString();

		boolean flag = true;

	Outer:	for(int i = 0; i < s.length();) {
			boolean flag2 = false; //4個の文字列のうちあてはまるものはあるか

			if(s.length() - i == 5) {
				if(s.substring(i,s.length()).equals("maerd") || s.substring(i,s.length()).equals("esare")) {
					flag2 = true;
					break Outer;
				}
			}
			if(s.length() - i == 6) {
				if(s.substring(i,s.length()).equals("resare")){
					flag2 = true;
					break Outer;
				}
			}

			for(int j = 0; j < 4; j++) {
				String d = sb[j].toString();

				if(s.substring(i,i+d.length()).equals(d)) { //あてはまった
					flag2 = true;
					i += d.length();
					continue Outer;
				}
			}

			if(!flag2) {
				flag = false;
				break;
			}
		}

		if(flag) {
			System.out.println("YES");
		}else {
			System.out.println("NO");
		}

	}

}

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Score : 300 points

Problem Statement

You are given a string S consisting of lowercase English letters.
Another string T is initially empty.
Determine whether it is possible to obtain S = T by performing the following operation an arbitrary number of times:

Append one of the following at the end of T: dream, dreamer, erase and eraser.

Constraints

1≦|S|≦10^5

S consists of lowercase English letters.

Input

The input is given from Standard Input in the following format:

S

Output

If it is possible to obtain S = T, print YES. Otherwise, print NO.

Sample Input 1

erasedream

Sample Output 1

YES

Append erase and dream at the end of T in this order, to obtain S = T.

Sample Input 2

dreameraser

Sample Output 2

YES

Append dream and eraser at the end of T in this order, to obtain S = T.

Sample Input 3

dreamerer

Sample Output 3

NO

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String str = sc.next();
		str = str.replaceAll("eraser", "-");
		str = str.replaceAll("erase", "-");
		str = str.replaceAll("dreamer", "-");
		str = str.replaceAll("dream", "-");

		str = str.replaceAll("-", "");

		if(str.length() == 0) {
			System.out.println("YES");
		} else {
			System.out.println("NO");
		}

	}
}


import java.util.*;
import java.io.*;

public class Main {
    static String[] word = {"dream", "dreamer", "erase", "eraser"};
    static int[] wordlen = {5,7,5,6};
    public static void main(String[] args) throws Exception {
        FastScanner sc = new FastScanner(System.in);
        String s = sc.next();
        int nowIndex = s.length();
        while(nowIndex > 0){
            boolean flg = false;
            for(int i = 0; i < 4; i++){
                if(nowIndex >= wordlen[i] && s.substring(nowIndex-wordlen[i],nowIndex).equals(word[i])){
                    nowIndex -= wordlen[i];
                    flg = true;
                    break;
                }
            }
            if(!flg){
                break;
            }
        }
        System.out.println(nowIndex == 0 ? "YES" : "NO");
    }
}

class FastScanner {
    private BufferedReader reader = null;
    private StringTokenizer tokenizer = null;
    public FastScanner(InputStream in) {
        reader = new BufferedReader(new InputStreamReader(in));
        tokenizer = null;
    }

    public String next() {
        if (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public String nextLine() {
        if (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                return reader.readLine();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken("\n");
    }

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

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

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

    public int[] nextIntArray(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = nextInt();
        return a;
    }

    public long[] nextLongArray(int n) {
        long[] a = new long[n];
        for (int i = 0; i < n; i++)
            a[i] = nextLong();
        return a;
    }
}


import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		String S = sc.nextLine();
		S = S.replaceAll("eraser","").replaceAll("erase","").replaceAll("dreamer","").replaceAll("dream","");

		System.out.println(S);

		if(S.length() == 0) {
			System.out.println("YES");
		}else {
			System.out.println("NO");
		}
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		if(br.readLine( ).matches("^((dreamer)|(eraser)|(dream)|(erase))+$") ){
			System.out.println("YES");
		}else{
			System.out.println("NO");
		}
	}
}


import java.util.*;
import java.util.stream.*;

public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
        // programming
        String s = getInputString(sc);
        StringBuilder temp = new StringBuilder(s);
        boolean isFound = false;
        String[] words = {"dream","dreamer","erase","eraser"};

        if (temp.length() == 0) {
            output("NO");
            return;
        }
        l1: while (true) {
            for (String w : words) {
                if (temp.toString().endsWith(w)) {
                    temp = temp.delete(temp.toString().length() - w.length(), temp.toString().length());
                    if (temp.toString().length() == 0) {
		                isFound = true;
		                break l1;
		            }
		            continue l1;
                }
            }

            break;
        }
        if (isFound) {
            output("YES");
        } else {
            output("NO");
        }
	}

	public static String getInputString(Scanner sc) {
		return sc.next();
	}

	public static int getInputInteger(Scanner sc) {
		return Integer.parseInt(sc.next());
	}

	public static void output(String... params) {
		boolean isInit = true;
		String str = "";
		for (String s : params) {
			if (!isInit) {
				str += " ";
			}
			str += s;
			isInit = false;
		}
		System.out.println(str);
	}

    public static int[] toIntegerList(String[] ss) {
        return Stream.of(ss).mapToInt(Integer::parseInt).toArray();
    }

    public static String[] toList(String s) {
        String[] ret = s.split("");
        return ret;
    }

    public static int sum(int[] vals) {
        int sum = 0;
        for (int val : vals) {
            sum += val;
        }
        return sum;
    }

    public static int[] sort(int[] vals, boolean isAsc) {
        Arrays.sort(vals);
		if (isAsc) {
			return vals;
		}
        int[] ret = new int[vals.length];
        for (int i = 0; i < vals.length; i++) {
            ret[vals.length - i - 1] = vals[i];
        }
		return ret;
	}
}


import java.util.Scanner;

/**
 *
 * @author psygn
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        String str = scanner.nextLine();
        String ans = "NO";
        str = str.replace("dreamer", "[space]");
        str = str.replace("dream", "[space]");
        str = str.replace("eraser", "[space]");
        str = str.replace("erase", "[space]");
        str = str.replace("[space]", "");
        if ("".equals(str)){
            ans = "YES";
        }
        System.out.println(ans);
    }
}


import java.util.ArrayList;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		String S=sc.next();
		if(S.length()<5)System.out.println("NO");
		StringBuilder s=new StringBuilder(S);
		ArrayList<String>de=new ArrayList<>();
		de.add("dream");
		de.add("dreamer");
		de.add("erase");
		de.add("eraser");
		while(true){
			if(de.contains(s.substring(0,s.length()))){System.out.println("YES");break;}
			if(s.length()<5 || s.length()==8 || s.length()==9){System.out.println("NO");break;}

			if((s.length()>5&&s.substring(0,6).equals("dreamd"))||(s.length()>7&&s.substring(0,8).equals("dreamera")))s.delete(0,5);
			else if(s.length()>=6&&(s.substring(0,6).equals("erasee")||s.substring(0,6).equals("erased")))s.delete(0,5);
			else if(s.length()>=7&&(s.substring(0,7).equals("erasere")||s.substring(0,7).equals("eraserd")))s.delete(0,6);
			else if(s.length()>=8&&(s.substring(0,8).equals("dreamerd")||s.substring(0,8).equals("dreamere")))s.delete(0,7);
			else {System.out.println("NO");break;}

		}
}
}


import java.util.Scanner;

public class Main {
	static boolean checkB[] = { true, true, true, true };

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		String s = sc.next();
		String tmp = null;
		while (true) {
			if(!s.equals(""))
				tmp = whatNext(s);
			if (s.equals("") || tmp == null) {
				if (s.equals(""))
					System.out.println("YES");
				else {
					System.out.println("NO");
				}
				return;
			}

			s = cut(s, tmp);
		}
	}

	static String cut(String tx, String cut) {
		int tmp = tx.indexOf(cut);
		tmp += cut.length();
		try {
			String tmpS = tx.substring(tmp);
			checkIn(tmpS);
			return tmpS;
		} catch (StringIndexOutOfBoundsException e) {
			for (int i = 0; i < 4; i++) {
				checkB[i] = false;
			}
			return "";
		}
	}

	static String whatNext(String tx) {
		try{
		char first = tx.charAt(0);
		if (checkB[0] && first == 'd') {
			// dreamあり
			if (!checkB[2] || tx.charAt(7) == 'a' || tx.charAt(5) == 'd' )
				return "dream";
			else
				return "dreamer";

		} else if (checkB[2] && first == 'e') { // eraseある
			if (tx.charAt(4) == 'e') {
				if (!checkB[0] || tx.charAt(5) == 'r' || tx.charAt(5) == 'e') {
					return "eraser";
				} else {
					return "erase";
				}
			} else {
				checkB[2] = false;
				checkB[3] = false;
				return null;
			}
		} else {
			return null;
		}
		}catch (StringIndexOutOfBoundsException e) {
			return null;
		}
	}
	static void checkIn(String tx) {
		if (checkB[0]) {
			if (!tx.contains("dream")) {
				checkB[0] = false;
				checkB[1] = false;
			}
		}
		if (checkB[1]) {
			if (!tx.contains("dreamer")) {
				checkB[1] = false;
			}
		}
		if (checkB[2]) {
			if (!tx.contains("erase")) {
				checkB[2] = false;
				checkB[3] = false;
			}
		}
		if (checkB[3]) {
			if (!tx.contains("eraser")) {
				checkB[3] = false;
			}
		}
	}
}


package src;

import java.util.*;
class Main{
	static Scanner s = new Scanner(System.in);
	public static void main(String[] args) {
		StringBuilder str = new StringBuilder(s.next()
				.replaceAll("eraser", "")
				.replaceAll("dream", "D")
				.replaceAll("er", "R")
				.replaceAll("as", "A"));
		String[] m = {"RAe","DR","D"};
		String b,st;
		boolean f;
		do {
			f=false;
			for(int i=0;i<m.length;i++) {
				b=m[i];
				st = str.toString();
				if(st.endsWith(b)) {
					str.delete(str.length()-b.length(),str.length());
					f=true;
					break;
				}
			}
		}while(f);
		System.out.println((str.toString().equals(""))?"YES":"NO");
	}
}


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

/**
 * Created by jaga on 9/22/16.
 */
public class Main{
    public static void solve(PrintWriter out, FastScanner in){
        //逆向きの文字列を考えてみる
        //dream dreamer erase eraser
        //maerd remaerd esare resare
        //最初の三文字で判定可能！
        String S = in.next();
        char c[] = S.toCharArray();
        char cr[] = new char[c.length];
        for(int i = 0;i < c.length;i++) {
            cr[i] = c[c.length - 1 - i];
        }

        S = String.valueOf(cr);

        int idx = 0;
        while (idx <S.length() - 1 ) {
            String str3 = S.substring(idx, idx + 3);
            String str5 = S.substring(idx, idx + 5);

            if (str3.equals("mae")) {
                if (str5.equals("maerd")) {
                    idx += 5;
                } else {
                    System.out.println("NO");
                    return;
                }
            } else if (str3.equals("rem")) {
                String str7 = S.substring(idx, idx + 7);
                if (str7.equals("remaerd")) {
                    idx += 7;
                } else {
                    System.out.println("NO");
                    return;
                }

            } else if (str3.equals("esa")) {
                if (str5.equals("esare")) {
                    idx += 5;
                } else {
                    System.out.println("NO");
                    return;
                }
            } else if (str3.equals("res")) {
                String str6 = S.substring(idx, idx + 6);
                if (str6.equals("resare")) {
                    idx += 6;
                } else {
                    System.out.println("NO");
                    return;
                }
            } else {
                System.out.println("NO");
                return;
            }
        }
        System.out.println("YES");

    }

    static class Pair implements Comparable {
        int first, second;

        Pair(int a, int b) {
            first = a;
            second = b;
        }

        public int compareTo(Object other) {
            Pair p1 = (Pair) other;
            return this.first - ((Pair) other).first; // IDの値に従い昇順で並び替えたい場合
//         return -(this.first - ((Pair) other).first); // IDの値に従い降順で並び替えたい場合
        }
    }

    /*以下はテンプレ*/
    public static void main(String args[]) {
        OutputStream outputStream = System.out;
        PrintWriter out = new PrintWriter(outputStream);
        FastScanner in = new FastScanner();
        solve(out, in);
        out.close();

    }

    public static class FastScanner {
        private final InputStream in = System.in;
        private final byte[] buffer = new byte[1024];
        private int ptr = 0;
        private int bufferLength = 0;

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

        private int readByte() {
            if (hasNextByte()) return buffer[ptr++];
            else return -1;
        }

        private static boolean isPrintableChar(int c) {
            return 33 <= c && c <= 126;
        }

        private void skipUnprintable() {
            while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
        }

        boolean hasNext() {
            skipUnprintable();
            return hasNextByte();
        }

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

        long nextLong() {
            if (!hasNext()) throw new NoSuchElementException();
            long n = 0;
            boolean minus = false;
            int b = readByte();
            if (b == '-') {
                minus = true;
                b = readByte();
            }
            if (b < '0' || '9' < b) {
                throw new NumberFormatException();
            }
            while (true) {
                if ('0' <= b && b <= '9') {
                    n *= 10;
                    n += b - '0';
                } else if (b == -1 || !isPrintableChar(b)) {
                    return minus ? -n : n;
                } else {
                    throw new NumberFormatException();
                }
                b = readByte();
            }
        }

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

        double[] nextDoubleArray(int n) {
            double[] array = new double[n];
            for (int i = 0; i < n; i++) {
                array[i] = nextDouble();
            }
            return array;
        }

        double[][] nextDoubleMap(int n, int m) {
            double[][] map = new double[n][];
            for (int i = 0; i < n; i++) {
                map[i] = nextDoubleArray(m);
            }
            return map;
        }

        public int nextInt() {
            return (int) nextLong();
        }

        public int[] nextIntArray(int n) {
            int[] array = new int[n];
            for (int i = 0; i < n; i++) array[i] = nextInt();
            return array;
        }

        public long[] nextLongArray(int n) {
            long[] array = new long[n];
            for (int i = 0; i < n; i++) array[i] = nextLong();
            return array;
        }

        public String[] nextStringArray(int n) {
            String[] array = new String[n];
            for (int i = 0; i < n; i++) array[i] = next();
            return array;
        }

        public char[][] nextCharMap(int n) {
            char[][] array = new char[n][];
            for (int i = 0; i < n; i++) array[i] = next().toCharArray();
            return array;
        }

        public int[][] nextIntMap(int n, int m) {
            int[][] map = new int[n][];
            for (int i = 0; i < n; i++) {
                map[i] = nextIntArray(m);
            }
            return map;
        }
    }
}


import java.util.*;

public class Main {
    public static void main(String[] args){

       Scanner sc = new Scanner(System.in);

        String S = sc.next();
		int N = S.length();

		int current = N;

		while(current > 0){
			if(current >= 7){
				String sub = S.substring(current-7, current);
				if(sub.equals("dreamer")){
					current -= 7;
					continue;
				}
			}

			if(current >= 6){
				String sub = S.substring(current-6, current);
				if(sub.equals("eraser")){
					current -= 6;
					continue;
				}
			}

			if(current >= 5){
				String sub = S.substring(current-5, current);
				if(sub.equals("dream") || sub.equals("erase")){
					current -= 5;
					continue;
				}
			}

			System.out.println("NO");
                        return;
		}

		System.out.println("YES");

  }
}


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.next();
        String []PARTS = {"dream","dreamer","erase","eraser"};
        boolean flag = true;
        boolean able = true;
        while(flag) {
            flag = false;
            if (input.equals("")) able =true;
            for (int i = 0; i < PARTS.length; i++) {
                if (input.endsWith(PARTS[i])) {
                    input = input.substring(0, input.length() - PARTS[i].length());
                    flag= true;
                }
            }
            if (input.equals("")) able =true;
            else able = false;
        }
        if(able){
            System.out.println("YES");
        }
        else{
            System.out.println("NO");
        }

        sc.close();

    }

//    private static boolean check(String s){
//        boolean flag = true;
//        while(flag) {
//            flag = false;
//            if (s.equals("")) return true;
//            for (int i = 0; i < PARTS.length; i++) {
//                if (s.endsWith(PARTS[i])) {
//                   s = s.substring(0, s.length() - PARTS[i].length());
//                    flag= true;
//                }
//            }
//        }
//        return false;
//    }

}


import java.util.Scanner;

public class Main {

    static String []PARTS = {"dream","dreamer","erase","eraser"};
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.next();
        if(check(input)){
            System.out.println("YES");
        }
        else{
            System.out.println("NO");
        }
        sc.close();

    }

    private static boolean check(String s){
        boolean flag = true;
        if(s.equals("")) return flag;
        for(int i=0;i<PARTS.length;i++){
            if(s.endsWith(PARTS[i])){
                return check(s.substring(0,s.length()-PARTS[i].length()));
            }
        }
        return false;
    }

}


import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    char[] s = scanner.next().toCharArray();
    int count = 0;
    for (int i = 1; i < s.length; i++) if (s[i] != s[i - 1]) count++;
    System.out.println(count);
  }
}

Score : 300 points

Problem Statement

Two foxes Jiro and Saburo are playing a game called 1D Reversi. This game is played on a board, using black and white stones. On the board, stones are placed in a row, and each player places a new stone to either end of the row. Similarly to the original game of Reversi, when a white stone is placed, all black stones between the new white stone and another white stone, turn into white stones, and vice versa.

In the middle of a game, something came up and Saburo has to leave the game. The state of the board at this point is described by a string S. There are |S| (the length of S) stones on the board, and each character in S represents the color of the i-th (1 ≦ i ≦ |S|) stone from the left. If the i-th character in S is B, it means that the color of the corresponding stone on the board is black. Similarly, if the i-th character in S is W, it means that the color of the corresponding stone is white.

Jiro wants all stones on the board to be of the same color. For this purpose, he will place new stones on the board according to the rules. Find the minimum number of new stones that he needs to place.

Constraints

1 ≦ |S| ≦ 10^5

Each character in S is B or W.

Input

The input is given from Standard Input in the following format:

S

Output

Print the minimum number of new stones that Jiro needs to place for his purpose.

Sample Input 1

BBBWW

Sample Output 1

1

By placing a new black stone to the right end of the row of stones, all white stones will become black. Also, by placing a new white stone to the left end of the row of stones, all black stones will become white.

In either way, Jiro's purpose can be achieved by placing one stone.

Sample Input 2

WWWWWW

Sample Output 2

0

If all stones are already of the same color, no new stone is necessary.

Sample Input 3

WBWBWBWBWB

Sample Output 3

9


import java.util.*;

public class Main {
  public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    String line = sc.nextLine();
    char[] c = line.toCharArray();
    int count = 0;
    for(int i = 0; i < c.length-1; i++) {
      if(c[i] != c[i+1]){
        count++;
      }
    }
    System.out.println(count);
  }
}


import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String S = sc.next();
    int count = 0;
    for(int i = 0; i < S.length() - 1; i++) {
      if(S.charAt(i) != S.charAt(i + 1)) count++;
    }
    System.out.println(count);
  }
}


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String S = sc.next();
        int n=0;
        char flag = ' ';
        for(int i=0;i<S.length();i++){
            if(S.charAt(i)==flag){

            }else{
                flag = S.charAt(i);
                n++;
            }
        }
        System.out.println(n-1);
        sc.close();

    }

}


import java.util.*;

/**
 * @author mix
 */
public class Main {

    public static void main(String[] args) {
        // TODO code application logic here
        Scanner s1 = new Scanner(System.in);
        int N = s1.nextInt();
        int L = s1.nextInt();
        String[] stringArray = new String[N];
        for (int i = 0; i < N; i++) {
            stringArray[i] = s1.next();
        }
        Arrays.sort(stringArray);
        for (String s : stringArray) {
            System.out.print(s);
        }
        System.out.println("");
    }

}

Score : 200 points

Problem Statement

Iroha has a sequence of N strings S_1, S_2, ..., S_N. The length of each string is L.

She will concatenate all of the strings in some order, to produce a long string.

Among all strings that she can produce in this way, find the lexicographically smallest one.

Here, a string s=s_1s_2s_3...s_n is lexicographically smaller than another string t=t_1t_2t_3...t_m if and only if one of the following holds:

There exists an index i(1≦i≦min(n,m)), such that s_j = t_j for all indices j(1≦j<i), and s_i<t_i.

s_i = t_i for all integers i(1≦i≦min(n,m)), and n<m.

Constraints

1 ≦ N, L ≦ 100

For each i, the length of S_i equals L.

For each i, S_i consists of lowercase letters.

Input

The input is given from Standard Input in the following format:

N L
S_1
S_2
:
S_N

Output

Print the lexicographically smallest string that Iroha can produce.

Sample Input 1

3 3
dxx
axx
cxx

Sample Output 1

axxcxxdxx

The following order should be used: axx, cxx, dxx.


import java.util.*;

public class Main {

    public static void main(String args[]) {

        // 入力
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        int l = Integer.parseInt(sc.next());
        String[] s = new String[n];
        for (int i = 0; i < n; i++) {
            s[i] = sc.next();
        }

        // 主処理
        Arrays.sort(s);
        String result = String.join("", s);

        // 出力
        System.out.println(result);
        sc.close();
    }
}


import java.util.*;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int strNum = sc.nextInt();
        int strLen = sc.nextInt();
        List<String> strList = scanList(sc,strNum);

        System.out.println(execute(strNum,strLen,strList));

    }

    public static String execute(int strNum, int strLen, List<String> strList) {
        strList.sort(Comparator.naturalOrder());
        StringBuffer buf = new StringBuffer();
        for(String str : strList){
            buf.append(str);
        }

        return buf.toString();
    }

    public static List<String> scanList(Scanner sc,int num){
        List<String> scanList = new ArrayList<>();
        for(int i = 0; i <= num ; i++){
            scanList.add(sc.nextLine());
        }
        return scanList;
    }
}


import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        int l = Integer.parseInt(sc.next());
        String[] ary = new String[n];
        for (int i = 0; i < n; i++) {
            String a = sc.next();
            ary[i] = a;
        }
        Arrays.sort(ary);
        for (String ans : ary) {
            System.out.print(ans);
        }
    }
}


import java.util.Scanner;
import java.util.Arrays;
import java.util.Collections;
public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    int L = sc.nextInt();
    String[] s = new String[N];
    for(int i = 0;i < N;i++){
      s[i] = sc.next();
    }
    Arrays.sort(s);
    for(int i = 0;i < N;i++){
      System.out.print(s[i]);
    }
  }
}


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class Main {

    public static void main(String[] args) {
        Scanner stdin = new Scanner(System.in);
        int n = stdin.nextInt();
        int l = stdin.nextInt();
        List<String> s = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            s.add(stdin.next());
        }

        String ans = s.stream().sorted().collect(Collectors.joining());
        System.out.println(ans);
    }

}


import java.util.Arrays;
import java.util.Scanner;

public class Main {
	private static Scanner sc = new Scanner(System.in);

	public static void main(String[] args) {
		String[] arr = new String[sc.nextInt()];
		sc.nextLine();
		for(int i = 0; i < arr.length; i++) {
			arr[i] = sc.nextLine();
		}
		Arrays.sort(arr);
		for(String e : arr) {
			System.out.print(e);
		}
		System.out.println();
	}
}


import java.util.*;
public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int L = sc.nextInt();
		String Sn[] = new String[N];
		for(int sGet = 0;sGet < N;sGet++){
			Sn[sGet] = sc.next();
			//char[] chars = Sn[sGet].toCharArray();
			//並べ替えを行う
			//Arrays.sort(chars);
			//String newSget = new String(chars);
			//Sn[sGet] = newSget;
			//System.out.println(Sn[sGet]);
		}
		Arrays.sort(Sn);
		for(int num = 0;num < Sn.length;num++){
			System.out.print(Sn[num]);
		}
		System.out.println();

	}
}


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        new Solver().solve(new Scanner(System.in));
//        new Solver().solve(new Scanner(ex));
    }

    private static final String ex = "3 3\n" +
            "dxx\n" +
            "axx\n" +
            "cxx";
}

class Solver {
    Solver() {}

    void solve(Scanner scanner) {
        int[] split = splitInt(scanner.nextLine());
        int N = split[0];
        int L = split[1];

        List<String> strs = new ArrayList<>();
        for (int i = 0; i < N; i ++) {
            strs.add(scanner.nextLine());
        }

        Collections.sort(strs);

        System.out.println(String.join("", strs));
    }

    private int[] splitInt(String s) {
        String[] split = s.split(" ");
        int[] splitInt = new int[split.length];
        for (int i = 0; i < split.length; i ++) {
            splitInt[i] = Integer.parseInt(split[i]);
        }
        return splitInt;
    }
}


import java.util.*;
import java.io.*;

public class Main {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int l = in.nextInt();
        String[] a = new String[n];
        for (int i = 0; i < n; ++i) {
            a[i] = in.next();
        }
        Arrays.sort(a);
        String s = "";
        for (int i = 0; i < n; ++i) {
            s += a[i];
        }
        System.out.println(s);
    }
}


import java.util.Arrays;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int L = sc.nextInt();
		String s[] = new String[N];
		for(int i = 0; i < N; i++){
			s[i] = sc.next();
		}
		Arrays.sort(s);
		for(int i = 0; i < N; i++){
			System.out.print(s[i]);
		}
		System.out.println("");
	}
}


import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main{
    public static void main(String args[]){
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        int L = scan.nextInt();
        String str[] = new String[N];
        String ans = "";

        Scanner scan2 = new Scanner(System.in);
        for(int i=0; i<N; i++){
            str[i] = scan2.nextLine();
        }

        Arrays.sort(str, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                    return o1.compareTo(o2);
                }
        });
        for(int i=0; i<N; i++){
            ans = ans + str[i];
        }
    System.out.println(ans);
    }
}


import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	static PrintWriter out = new PrintWriter(System.out);
	public static void main(String[] args) {
		// 入力
		final int n, l;
		final String[] s;
		try(Scanner scan = new Scanner(System.in)) {
			n = scan.nextInt();
			l = scan.nextInt();
			s = new String[n];
			for (int i = 0; i < n; i++) {
				s[i] = scan.next();
			}

		}
		Arrays.sort(s);

		// 出力
		Arrays.stream(s).forEach(out::print);
		out.println();
		out.flush();
	}

}