<?php
  list($s) = explode(" ", trim(fgets(STDIN)));

  $s = strrev($s);

  while($s != ""){
    if(substr($s, 0, 5) == 'maerd'){
      $s = substr($s, 5);
      continue;
    }
    if(substr($s, 0, 7) == 'remaerd'){
      $s = substr($s, 7);
      continue;
    }
    if(substr($s, 0, 5) == 'esare'){
      $s = substr($s, 5);
      continue;
    }
    if(substr($s, 0, 6) == 'resare'){
      $s = substr($s, 6);
      continue;
    }
    echo "NO";
    exit;
  }
  echo "YES";


<?php
  list($s) = explode(" ", trim(fgets(STDIN)));

  $s = strrev($s);

  if(f($s)){
    echo "YES";
  }else{
    echo "NO";
  }

  function f($s){
    if($s == ""){
      return true;
    }
    if(substr($s, 0, 5) == 'maerd'){
      if(f(substr($s, 5))){
        return true;
      }
    }
    if(substr($s, 0, 7) == 'remaerd'){
      if(f(substr($s, 7))){
        return true;
      }
    }
    if(substr($s, 0, 5) == 'esare'){
      if(f(substr($s, 5))){
        return true;
      }
    }
    if(substr($s, 0, 6) == 'resare'){
      if(f(substr($s, 6))){
        return true;
      }
    }
    return false;
  }

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.


<?php
$s = strrev(trim(fgets(STDIN)));
$divide = ['dream', 'dreamer', 'erase', 'eraser'];
foreach ($divide as $k => $v) {
    $divide[$k] = strrev($v);
}
$can = true;
for ($i = 0; $i < strlen($s);) {
    $can2 = false;
    foreach ($divide as $v) {
        if (strpos($s, $v, $i) === $i) {
            $can2 = true;
            $i += strlen($v);
        }
    }
    if (! $can2) {
        $can = false;
        break;
    }
}
echo $can ? 'YES' : 'NO';
?>


<?php
$S = trim(fgets(STDIN));
$T = '';

while (1) {
    if (strpos($S, 'eraser')!==false) {
        $S = str_replace('eraser', '', $S);
    }elseif (strpos($S, 'erase')!==false) {
        $S = str_replace('erase', '', $S);
    }elseif (strpos($S, 'dreamer')!==false) {
        $S = str_replace('dreamer', '', $S);
    }elseif (strpos($S, 'dream')!==false) {
        $S = str_replace('dream', '', $S);
    }
    else {
        break;
    }
}

echo $S==$T ? 'YES': 'NO';


<?php
$S = strrev(trim(fgets(STDIN)));
$T = '';

while (1) {
    if (strpos($S, 'maerd')!==false) {
        $S = str_replace('maerd', '', $S);
    }elseif (strpos($S, 'remaerd')!==false) {
        $S = str_replace('remaerd', '', $S);
    }elseif (strpos($S, 'esare')!==false) {
        $S = str_replace('esare', '', $S);
    }elseif (strpos($S, 'resare')!==false) {
        $S = str_replace('resare', '', $S);
    }else {
        break;
    }
}
if ($S == $T){
    print 'YES';
}else {
    print 'NO';
}


<?php
$S = trim(fgets(STDIN));
$T = '';
$a = '';

while (1) {
    if (strpos($S, 'dream')) {
        $S = str_replace('dream', '', $S);
    }elseif (strpos($S, 'dreamer')) {
        $S = str_replace('dreamer', '', $S);
    }elseif (strpos($S, 'erase')) {
        $S = str_replace('erase', '', $S);
    }elseif (strpos($S, 'eraser')) {
        $S = str_replace('eraser', '', $S);
    }else {
        break;
    }
}
if ($S == $T){
    print 'YES';
}else {
    print 'NO';
}


<?php
fscanf(STDIN, '%s', $s);

$assets = ['dreamer','dream','eraser','erase'];

foreach ($assets as $word) {
    if (strncmp($s,$word, strlen($word)) > -1) {
        $s = ltrim($s, $word);
    }
}

if ($s === '') {
    echo "YES".PHP_EOL;
} else {
    echo "NO".PHP_EOL;
}


<?php
fscanf(STDIN, "%s", $s);
$array = array('dream', 'dreamer', 'erase', 'eraser');

$s = strrev($s);
$array = array_map('strrev', $array);

while($s[4]){
    if(in_array(substr($s, 0, 5), $array)){
        $s = substr($s, 5);
    }elseif(in_array(substr($s, 0, 6), $array)){
        $s = substr($s, 6);
    }elseif(in_array(substr($s, 0, 7), $array)){
        $s = substr($s, 7);
    }else{
        break;
    }
}
if(strlen($s) === 0){
    print 'YES';
}else{
    print 'NO';
}


<?php
fscanf(STDIN, "%s", $s);
$len = strlen($s);
$array = array('dream', 'dreamer', 'erase', 'eraser');
while(strlen($s)>7){
    if(in_array(substr($s, -5), $array)){
        $s = substr($s, 0, ($len-5));
    }elseif(in_array(substr($s, -6), $array)){
        $s = substr($s, 0, ($len-6));
    }elseif(in_array(substr($s, -7), $array)){
        $s = substr($s, 0, ($len-7));
    }else{
        break;
    }
}
if(in_array($s, $array)){
    print 'YES';
}else{
    print 'NO';
}


<?php
$s = trim(fgets(STDIN));
$t = "";
$pattern = "/^(erase|dream|eraser|dreamer)*$/";
$ans = preg_match($pattern, $s) ? "YES" : "NO";
echo $ans . "\n";
?>


<?php
$inS = fgets(STDIN);
$inS = str_replace(array("\r\n","\r","\n"), "", $inS);

while(true) {
  $tem2 = substr($inS, -2, 2);
  $tem5 = substr($inS, -5, 5);
  $tem6 = substr($inS, -6, 6);
  $tem7 = substr($inS, -7, 7);
  if ($tem2 == 'er') {
    if ($tem7 == 'dreamer') {
      $inS = preg_replace("/dreamer\$/", '', $inS);
      continue;
    } elseif ($tem6 == 'eraser' ) {
      $inS = preg_replace("/eraser\$/", '', $inS);
      continue;
    } else {
      break;
    }
  } elseif ($tem5 == 'dream') {
      $inS = preg_replace("/dream\$/", '', $inS);
      continue;
  } elseif ($tem5 == 'erase') {
      $inS = preg_replace("/erase\$/", '', $inS);
      continue;
  }
  break;
}

echo strlen($inS) === 0 ? 'YES' : 'NO';

?>


<?php

//$_input = explode(
//    "\n",
//    'dreamerasererasedream
//'
//);

function getNext() {
    return trim(
        isset($GLOBALS['_input'])
            ? array_shift($GLOBALS['_input'])
            : fgets(STDIN)
    );
}

function buildable($s, $arr) {
    while (true) {
        foreach ($arr as $value) {
            if (strpos($s, $value) === 0) {
                $s = substr($s, strlen($value));

                if ($s === '') {
                    return true;
                }

                continue 2;
            }
        }

        return false;
    }
}

printf("%s\n", buildable(strrev(getNext()), array_map(function ($s) { return strrev($s); }, ['dream', 'dreamer', 'erase', 'eraser'])) ? 'YES' : 'NO');


<?php
$words = ["dream", "dreamer", "erase", "eraser"];

fscanf(STDIN, "%s", $S);

while($S) {
    $flg = false;

    foreach ($words as $word) {
        $len = strlen($word);
        $exam = substr($S, -$len);
        if ($exam === $word) {
            $S = substr($S, 0, strlen($S) - $len);
            $flg = true;
            break;
        }
    }

    if ($flg === false) break;
}

echo $S === "" ? "YES\n" : "NO\n";


<?php

$string = readline();

$string = str_replace(array('erase','eraser','dream','dreamer'), '', $string);

if (strlen($string) == 0)
{
    echo "yes";
}
 else
{
     echo "no";
}

echo "\n";


<?php
fscanf(STDIN, "%s", $s);
if (preg_match('/^(dream|dreamer|erase|eraser)+$/', $s)) {
	echo 'YES';
} else {
	echo 'NO';

}


<?php

$stdin = fopen('php://stdin', 'r');

$buf = fgets($stdin);
$buf = trim($buf);

while (true) {
    if ($buf == '') {
        echo 'YES';
        exit();
    }

    if (preg_match('/dreamer$/', $buf)) {
        $buf = substr($buf, 0, strlen($buf) - 7);
        continue;
    }
    if (preg_match('/dream$/', $buf)) {
        $buf = substr($buf, 0, strlen($buf) - 5);
        continue;
    }
    if (preg_match('/eraser$/', $buf)) {
        $buf = substr($buf, 0, strlen($buf) - 6);
        continue;
    }
    if (preg_match('/erase$/', $buf)) {
        $buf = substr($buf, 0, strlen($buf) - 5);
        continue;
    }

    echo 'NO';
    exit();
}

echo 'NO';


<?php

$stdin = fopen('php://stdin', 'r');

$buf = fgets($stdin);
$buf = trim($buf);

while (true) {
    if ($buf == '') {
        echo 'YES';
        exit();
    }

    if (preg_match('/^dreamer/', $buf)) {
        $buf = substr($buf, 7, 10000);
        continue;
    }
    if (preg_match('/^dream/', $buf)) {
        $buf = substr($buf, 5, 10000);
        continue;
    }
    if (preg_match('/^eraser/', $buf)) {
        $buf = substr($buf, 6, 10000);
        continue;
    }
    if (preg_match('/^erase/', $buf)) {
        $buf = substr($buf, 5, 10000);
        continue;
    }

    echo 'NO';
    exit();
}

echo 'NO';


<?php
list($s) = strs();
$cnt = 0;
$n = strlen($s);
for ($i = 1; $i < $n; ++$i) if ($s[$i] !== $s[$i - 1]) $cnt++;
echo $cnt;
function strs()
{
    return explode(' ', trim(fgets(STDIN)));
}

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


<?php
$line = fgets(STDIN);
echo (substr_count($line, "BW") + substr_count($line, "WB"));


<?php
    $splitted = str_split(trim(fgets(STDIN)));
//    $testcase = "WWWWW";
//    $splitted = str_split($testcase);
    $judge = $splitted[0];
    $num_change = 0;

    for($i = 1; $i < count($splitted); $i++){
//        echo $judge." next:",$splitted[$i]." count:".$num_change;
//        echo PHP_EOL;
        if($judge == $splitted[$i]){
        } else {
            $num_change++;
            $judge = $splitted[$i];
        }
    }
    echo $num_change;

?>


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.


<?php
list($n, $l) = ints();
for ($i = 0; $i < $n; ++$i) list($s[]) = strs();
natsort($s);
echo implode('', $s);
function strs()
{
    return explode(' ', trim(fgets(STDIN)));
}
function ints()
{
    return array_map('intval', explode(' ', trim(fgets(STDIN))));
}


<?php
// Your code here!
$input = explode(" ",trim(fgets(STDIN)));
// var_dump($input);
$n = $input[0];
$cnt = $input[1];

for($i=0; $i<=$n-1; $i++){
    $sum = trim(fgets(STDIN));
    $total[] = $sum;
}
sort($total);

for($j=0; $j<=$cnt-1;$j++){
    echo $total[$j];
}

?>


<?php

$arr = [];
while ($line = fgets(STDIN)){
    array_push($arr, $line);
};
unset($arr[0]);
sort($arr);
$string = implode("", $arr);
echo $string;


<?php

  fscanf(STDIN,"%d%d",$n,$l);
$s_li=array();
for($i=0;$i<$n;$i++){
  $s_li[]=trim(fgets(STDIN));
}

//print_r($s_li);

sort($s_li);
//print_r($s_li);
$re = '';
foreach($s_li as $val){
  $re.=$val;
}

echo $re."\n";


<?php
  $N = 0;
  $L = 0;
  $strArray = [];
  $count = 0;
  while ( ! feof(STDIN) ) {
    $line = trim(fgets(STDIN));
    if ($count === 0) {
      $tmp = explode(" ", $line);
      $N = $tmp[0];
      $L = $tmp[1];
    } else {
      $strArray[] = $line;
    }
    $count++;
  }
  sort($strArray);
  $str = "";
  for ($i = 0; $i < $L; $i++) {
    $str .= $strArray[$i];
  }
  echo $str;


<?php
  $N = 0;
  $L = 0;
  $strArray = [];
  $count = 0;
  while ( ! feof(STDIN) ) {
    $line = trim(fgets(STDIN));
    if ($count === 0) {
      $tmp = explode(" ", $line);
      $N = $tmp[0];
      $L = $tmp[1];
    } else {
      $strArray[] = $line;
    }
    $count++;
  }
  sort($strArray);
  for ($i = 0; $i < $L; $i++) {
    echo $strArray[$i];
  }


<?php

fscanf(STDIN, '%d %d', $n,$l);

$s = [];
for ($i=0;$i<$n;$i++) {
    fscanf(STDIN, '%s', $input);
    $s[] = $input;
}

sort($s);

echo implode('',$s).PHP_EOL;


<?php
fscanf(STDIN,"%d %d",$n,$l);
for($i=0; $i<$n; $i++)
{
    $s[$i] = rtrim(fgets(STDIN));
}
sort($s);

$result = "";
for($i=0; $i<$n; $i++)
{
    $result .= $s[$i];
}

echo($result);


<?php
fscanf(STDIN, "%d %d", $n, $l);
$l = [];
for ($i=0; $i < $n; $i++) {
    fscanf(STDIN, "%s", $s);
    $l[] = $s;
}
sort($l);
foreach ($l as $v) {
    echo $v;
}


<?php
    fscanf(STDIN, "%d %d", $n, $l);
    $str_list = [];
    for ($i = 0; $i < $n; $i++) {
        fscanf(STDIN, "%s", $str_list[]);
    }

    for ($i = 1; $i < $l; $i++) {
        for ($j = 0; $j < $l-$i; $j++) {
            for ($k = 0; $k < $l; $k++) {
                $a = substr($str_list[$j], $k, 1);
                $b = substr($str_list[$j+1], $k, 1);

                if ($a == $b) {
                    continue;
                } elseif ($a > $b) {
                    $temp = $str_list[$j];
                    $str_list[$j] = $str_list[$j+1];
                    $str_list[$j+1] = $temp;
                    break;
                } else {
                    break;
                }
            }
        }
    }

    $result = "";

    foreach ($str_list as $str) {
        $result .= $str;
    }

    echo $result;
?>


<?php
    fscanf(STDIN, "%d %d", $n, $l);
    $str_list = [];
    for ($i = 0; $i < $n; $i++) {
        fscanf(STDIN, "%s", $str_list[]);
    }

    print_r($str_list);

    for ($i = 1; $i < $l; $i++) {
        for ($j = 0; $j < $l-$i; $j++) {
            for ($k = 0; $k < $l; $k++) {
                $a = substr($str_list[$j], $k, 1);
                $b = substr($str_list[$j+1], $k, 1);
                echo $a." ".$b."\n";
                if ($a == $b) {
                    continue;
                } elseif ($a > $b) {
                    $temp = $str_list[$j];
                    $str_list[$j] = $str_list[$j+1];
                    $str_list[$j+1] = $temp;
                    echo "\n";
                    break;
                } else {
                    break;
                }
            }
        }
    }

    print_r($str_list);

    $result = "";

    foreach ($str_list as $str) {
        $result .= $str;
    }

    echo $result;
?>


<?php
$s=fgets(STDIN);
$s=explode(" ",$s);
for($i=0;$i<$s[1];$i++){
    $arr[]=fgets(STDIN);
}
sort($arr);
$arr=implode($arr);
$arr=str_replace("\n","",$arr);

echo $arr;


<?php
$s=fgets(STDIN);
$s=explode(" ",$s);
for($i=0;$i<$s[1];$i++){
    $arr[]=fgets(STDIN);
}
sort($arr);
$arr=implode($arr);
$arr=str_replace("\n","",$arr);

var_dump($arr);


<?php
  fscanf(STDIN,"%d %d", $n, $l);

  $arr[] = 0;
  for($i = 0; $i < $n; $i++) {
    fscanf(STDIN, "%s", $s);
    $arr[$i] = $s;
  }

  sort($arr);
  $string = "";

  foreach($arr as $ar) {
    $string .= $ar;
  }

  echo $string;

?>


<?php
    $n;
    $l;
    $ar;
    fscanf(STDIN, "%d %d", $n, $l);
    for($i = 0; $i < $n; $i++){
        $s;
        fscanf(STDIN, "%s", $s);
        $ar[] = $s;
    }
    sort($ar, SORT_STRING);
    foreach($ar as $s){
        echo $s;
    }
    echo "\n";
?>


<?php
fscanf(STDIN, "%d %d", $length, $max);

$textList = array();
for( $i=0; $i<$max; $i++ )
{
  $textList[] = trim(fgets(STDIN));
}
sort( $textList );

$result = implode( "", $textList );
echo $result;
?>


<?php
    $str = "";
    $cnt = 0;
    $s=rtrim(fgets(STDIN));
    $st = array();
    while($s=rtrim(fgets(STDIN))) {
        $st[$cnt]=$s;
        $cnt++;
    }
    sort($st);
    for($i=0;$i<sizeof($st);$i++) {
        echo "$st[$i]";
    }
    echo "\n";


<?php
    $str = "";
    $cnt = 0;
    $s=rtrim(fgets(STDIN));
    $n=$s[0];
    $st = array();
    while($s=rtrim(fgets(STDIN))) {
        $st[$cnt]=$s;
        $cnt++;
    }
    sort($st);
    for($i=0;$i<$n;$i++) {
        echo "$st[$i]";
    }
    echo "\n";


<?php
    $str = "";
    $cnt = 0;
    $s=rtrim(fgets(STDIN));
    $n=$s[0];
    $st = array();
    while($s=rtrim(fgets(STDIN))) {
        $st[$cnt]=$s;
        $cnt++;
    }
    sort($st);
    for($i=0;$i<$n;$i++) {
        echo "$st[$i]";
    }
    echo PHP_EOL;