C - 白昼夢 / Daydream
Time limit : 2sec / Memory limit : 256MB
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
anderaser
.
Constraints
- 1≦|S|≦105
- 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
//给一个字符串,问,能否只通过加 dream
dreamer
erase
eraser 这四个字符串得到
模拟就只要一重循环就够了,每次都确定连续的单词到底是什么,
1 #include <stdio.h> 2 #include <string.h> 3 4 char str [100005]; 5 char dr[10]="dream"; 6 char er[10]="erase"; 7 8 int main() 9 { 10 while (scanf("%s",&str)!=EOF) 11 { 12 int len = strlen (str); 13 int i,ok=1; 14 for (i=0;i<len;i++)//dreameraser 15 { 16 if (str[i]=='d') 17 { 18 int j; 19 for (j=i;j<=i+4;j++) 20 { 21 if (j>=len||dr[j-i]!=str[j]) 22 { 23 ok=0; 24 break; 25 } 26 } 27 if (j==i+5)//是dream 28 { 29 i=j-1; 30 if (str[j]=='e'&&str[j+1]=='r'&&str[j+2]!='a') 31 { 32 i=j+1; 33 } 34 } 35 } 36 else if (str[i]=='e') 37 { 38 int j; 39 for (j=i;j<=i+4;j++) 40 { 41 if (j>=len||er[j-i]!=str[j]) 42 { 43 ok=0; 44 break; 45 } 46 } 47 if (j==i+5) //是erase 48 { 49 i=j-1; 50 if (str[j]=='r') i=j; 51 else i=j-1; 52 } 53 } 54 else ok=0; 55 if (ok==0) break; 56 } 57 if (ok==1) 58 printf("YES "); 59 else 60 printf("NO "); 61 } 62 return 0; 63 }