不准访问
- 积分
- 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) ;
- void solu(string &S, string::size_type lastpos, int indentnum, vector<string> &st, int &max) ;
- int solution(string &S) {
.google и - // 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;
- }
- }-baidu 1point3acres
- return ret;.1point3acres
- }.google и
- int larger(vector<string> &st) {
- int len = 0;. ----
- for (int i = 0; i < st.size(); ++i) {. 1point 3 acres
-
- string w = st[i];
- len += w.length();
- }
- //cout << len << endl;
- return len;. Χ
- }
- -baidu 1point3acres
- bool isimage(string &s) {
- int pos = s.find('.');
- if (pos == string::npos) {
- return false;
- }
- string w = s.substr(pos+1);. check 1point3acres for more.
- //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('.');. 1point3acres.com
- if (pos == string::npos) {
- return false;
- } else
- return true;
- }
- void solu(string &S, string::size_type lastpos, int indentnum, vector<string> &st, int &max) {
- int i = lastpos;
- while (i < S.size()) {
- int n = S.find("\n", i);. check 1point3acres for more.
- string word = S.substr(i, n-i);
- if (numspace(word) == indentnum) {
- word = word.substr(indentnum);
- if (isdoc(word)) {. check 1point3acres for more.
- st.push_back(word);
- if (larger(st) > max && isimage(word)) {. .и
- max = larger(st);
- for (int i = 0; i < st.size(); ++i) {
- cout << st[i] << " ";
- }
- cout << endl;. .и
- }
- st.pop_back();
- } else {.--
- st.push_back(word);
- // cout << word << endl;
- st.push_back("/");. 1point 3acres
- solu(S, n+1, indentnum+1, st, max);
- st.pop_back();
- st.pop_back();
- }
- } else if (numspace(word) < indentnum) {. 1point3acres.com
- return;
- }
- .1point3acres
- i = n+1;
- }
- }. Waral dи,
- 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;
- ..
- }
复制代码 |
|