不准访问
- 积分
- 66
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2014-5-14
- 最后登录
- 1970-1-1
|
第二题的 c++ 解法
. ----
- #include <iostream>. Χ
- #include <string>. check 1point3acres for more.
- #include <vector>. From 1point 3acres bbs
- using namespace std;. From 1point 3acres bbs
- int numspace(string &s) ;
- int larger(vector<string> &st) ;
- bool isimage(string &s) ;
- void solu(string &S, string::size_type lastpos, int indentnum, vector<string> &st, int &max) ;
- int solution(string &S) {
- // write your code in C++
- int max = -1;. 1point3acres.com
- vector<string> st;
- //st.push_back("/");
- . 1point 3 acres
- solu(S, 0, 0, st, max);
- return max;
-
- }
- . 1point3acres.com
- int numspace(string &s) {
- int ret = 0;
- for (int i = 0; i < s.size();++i) {
.google и - if (s[i] == ' ') {. check 1point3acres for more.
- ret++;
- } else {
- break; ..
- }
- }
- return ret;
- }
- int larger(vector<string> &st) {
- int len = 0;
- for (int i = 0; i < st.size(); ++i) {.google и
-
- string w = st[i];
- len += w.length();
- }
- //cout << len << endl;
- return len;
- }
- bool isimage(string &s) {
- int pos = s.find('.');
- if (pos == string::npos) {-baidu 1point3acres
- return false;
- }
- string w = s.substr(pos+1);
- //cout << w << endl;
- if (w == "jpeg" || w == "gif"). 1point3acres
- return true;
- else
- return false;
-
- }. ----
- bool isdoc(string &s) {.google и
- int pos = s.find('.');
- if (pos == string::npos) {
- return false;
- } else ..
- return true;. From 1point 3acres bbs
- }
- void solu(string &S, string::size_type lastpos, int indentnum, vector<string> &st, int &max) {
- int i = lastpos;. 1point 3acres
- while (i < S.size()) {
- int n = S.find("\n", i);
- string word = S.substr(i, n-i);
- if (numspace(word) == indentnum) {
- word = word.substr(indentnum);
- if (isdoc(word)) {
- st.push_back(word);. 1point 3acres
- if (larger(st) > max && isimage(word)) {
- max = larger(st);
- for (int i = 0; i < st.size(); ++i) {
- cout << st[i] << " ";
- }
- cout << endl;
. 1point 3 acres - }
- st.pop_back();
- } else {
- st.push_back(word);
- // cout << word << endl;
- st.push_back("/");
- solu(S, n+1, indentnum+1, st, max);
- st.pop_back();
- st.pop_back();
- }
- } else if (numspace(word) < indentnum) {
- return;
- }
-
- i = n+1;
- }.--
- }
- int main() {
- string res = "dir\n dsfdsdfs\n xxxx.giff\ndir1\n dir2\n i.gif\n dir3\n ppp.jpeg\n";
- cout << solution(res) << endl; . 1point 3 acres
- }
复制代码 |
|