题意:s是否由给定的几个字符串组成。
思路:因为字符串的结尾只能是“r”,”m”,”e”.所以可以从后往前判断。If不是以这这为结尾就输出“NO”,否则截取字符串来比较。
知识点:substr(size_type _Off = 0,size_type _Count = npos)
一种构造string的方法
形式 : s.substr(pos, len)
返回值: string,包含s中从pos开始的len个字符的拷贝(pos的默认值是0,len的默认值是s.size() - pos,即不加参数会默认拷贝整个s)
异常 :若pos的值超过了string的大小,则substr函数会抛出一个out_of_range异常;若pos+n的值超过了string的大小,则substr会调整n的值,只拷贝到string的末尾
注意:pos不能小于0;
AC代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<string>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
const ll maxn = 2e5 + 10;
int main()
{
string ss;
while (cin >> ss)
{
for (ll i = ss.size() - 1; i >= 0;--i)
{
if (ss[i] == 'r')
{
string s2 = "sd", s1 = "as";
if(i>=5)
s1 = ss.substr(i - 5, 6);//在字符结尾的位置,截取整个字符时位置前移的距离为字符串长度减1
if (i >= 6)
s2 = ss.substr(i - 6, 7);
if (s1 != "eraser"&&s2!="dreamer")
{
cout << "NO" << endl;
return 0;
}
if (s1 == "eraser")
i = i - 5; //因为在循环中会再减1
if (s2 == "dreamer")
i = i - 6;
}
else if (ss[i] == 'e')
{
string s1 = "as";
if(i>=4)
s1 = ss.substr(i - 4, 5);
if (s1 != "erase")
{
cout << "NO" << endl;
return 0;
}
i = i - 4;
}
else if (ss[i] == 'm')
{
string s1 = "as";
if (i >= 4)
s1 = ss.substr(i - 4, 5);
if (s1 != "dream")
{
cout << "NO" << endl;
return 0;
}
i = i - 4;
}
else //不用这几个字母结尾的话一定是错的
{
cout << "NO" << endl;
return 0;
}
}
cout << "YES" << endl;
}
return 0;
}

1472

被折叠的 条评论
为什么被折叠?



