Daydream
題意:s是否由給定的幾個字符串組成。
思路:因為字符串的結(jié)尾只能是“r”,”m”,”e”.所以可以從后往前判斷。If不是以這這為結(jié)尾就輸出“NO”,否則截取字符串來比較。
知識點:substr(size_type _Off = 0,size_type _Count = npos)
一種構(gòu)造string的方法
形式 : s.substr(pos, len)
返回值: string,包含s中從pos開始的len個字符的拷貝(pos的默認值是0,len的默認值是s.size() - pos,即不加參數(shù)會默認拷貝整個s)
異常 :若pos的值超過了string的大小,則substr函數(shù)會拋出一個out_of_range異常;若pos+n的值超過了string的大小,則substr會調(diào)整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);//在字符結(jié)尾的位置,截取整個字符時位置前移的距離為字符串長度減1if (i >= 6)s2 = ss.substr(i - 6, 7);if (s1 != "eraser"&&s2!="dreamer"){cout << "NO" << endl;return 0;}if (s1 == "eraser")i = i - 5; //因為在循環(huán)中會再減1if (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 //不用這幾個字母結(jié)尾的話一定是錯的{cout << "NO" << endl;return 0;}}cout << "YES" << endl;}return 0; } 與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
- 上一篇: Pavel and Triangles(
- 下一篇: 用python操作MySQL