求助10.9滴滴笔试第一题怎么做?

就是字符串化简的那个, 想不出什么好的办法。。
我当时想的是 从短到长枚举重复的长度 但是如何处理嵌套 以及嵌套了怎么处理呢?

最后直接输出样例和原长度 还A了40% ,开心
#滴滴#
全部评论
我输出了3个样例60%。。。
点赞 回复 分享
发布于 2017-10-09 21:13
也是60%
点赞 回复 分享
发布于 2017-10-09 21:19
。。。写了半天A0
点赞 回复 分享
发布于 2017-10-09 21:19
#include <iostream> #include <string> #include <algorithm> using namespace std; string f(const string& str, int cur, int len, int minLen) { int m = 1; string s = str.substr(cur, len); //cout << s << endl; for (int i = len + cur; i <= str.size()-len; i += len) { string tmp = str.substr(i, len); //cout << tmp << endl; if (s != tmp) break; ++m; } int ret = str.size() - m*len + len + 3; if (ret < minLen) { string tt(str.begin(), str.begin() + cur); tt += to_string(m); tt += '['; tt += s; tt += ']'; string tmp(str.begin() + cur + m*len, str.end()); tt += tmp; return tt; } else return ""; } int main() { string str; cin >> str; int len = str.size(); if (len < 5) { cout << len << endl; return 0; } int cur = 0; int ret = len; string newStr = str; while (true) { bool changed = false; for (int cur = 0; cur < str.size() - 4; ++cur) { //i位子串的长度 for (int i = 1; i <= str.size() / 2; ++i) { //cout << f(str, cur, i) << endl; string ff = f(str, cur, i, ret); if (ff != "") { ret = ff.size(); newStr = ff; changed = true; } } } str = newStr; if (!changed || str.size() < 5) break; } cout << ret << endl; }
点赞 回复 分享
发布于 2017-10-09 21:20
#include <iostream> #include <string> using namespace std; string helper(string str) {  if (str.size() <= 4) {   return str;  }  for (int i = 0; i < str.size(); ++i) {   for (int j = str.size() - 1; j > i; --j) {    if (str[j] != str[i]) continue;    int k = 0;    while (j + k < str.size() && str[i + k] == str[j + k]) ++k;    string base = str.substr(i, k);    int len = j - i;    if (len % k != 0) continue;    int num = len / k + 1;    if (3 + k >= j - i + k) continue;    int n = num - 1;    while (--n) {     if (base == str.substr(i + n*k, k)) continue;     else break;    }    if (n != 0) continue;    else {     string res = str.substr(0, i);     string numstr = to_string(num);     res += numstr;     res += '[';     string next = helper(base);     res += next;     res += ']';     res += str.substr(j + k);     return res;    }   }  } } int main() {  string str;  cin >> str;  if (str.size() <= 4) {   cout << str.size() << endl;   return 0;  }  cout << helper(str).size() << endl;  system("pause");  return 0; }
点赞 回复 分享
发布于 2017-10-09 21:29
# -*- coding: UTF-8 -*- import re regex = r'(.{2,}?)(\1)+' expr = re.compile(regex) def repl(matched):     v0 = matched.group()     v2 = matched.group(2)     return str(len(v0) / len(v2)) + '['+ v2 + ']' def encode(string):     if string :         encoded = re.sub(expr, repl, string)         if len(string) > len(encoded):             encoded = encode(encoded)         else:             return string         return encoded     else:         print 'None string'      if __name__ == '__main__':     str1 = input('input encoding string')     print encode(str1)
点赞 回复 分享
发布于 2017-10-11 20:35
#include<memory> #include<string> #include<iostream> #include<vector> using namespace std; int code(string s) {     if (s.size() < 4)         return s.size();     vector<pair<string, int>> k;     for (size_t i = 1; i <= s.size() / 2; i++) {         string tmp(s.begin(), s.begin() + i);         k.push_back(make_pair(tmp, 1));     }     for (auto it = k.begin(); it != k.end(); it++) {         size_t len = (it->first).size();         for (size_t m = len; m+len<s.size()+1; m += len) {             string tmp(s.begin() + m, s.begin() + m + len);             if (it->first == tmp)                 it->second++;             else                 break;         }     }     auto max_index = k.end();     size_t max = 0;     for (auto it = k.begin(); it != k.end(); it++) {         if (it->second >= 2) {             size_t len = ((it->first).size())*(it->second);             if (len > max && (len > (it->first).size() + 3)) {                 max_index = it;                 max = len;             }         }     }     if (max_index != k.end()) {         string tmp(s.begin() + max, s.end());         return (code(max_index->first) + 3 + code(tmp));     }     else {         string tmp(s.begin() + 1, s.end());         return (1 + code(tmp));     }     return s.size(); } int main() {     string in;     cin >> in;     cout << code(in);     return 0; }
点赞 回复 分享
发布于 2017-10-11 20:59

相关推荐

字节 飞书绩效团队 (n+2) * 15 + 1k * 12 + 1w
点赞 评论 收藏
分享
shtdbb_:还不错,没有让你做了笔试再挂你
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务