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