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