查看: 3001| 回复: 3
跳转到指定楼层
上一主题 下一主题
收起左侧

表达式加括号使和最大

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
Give you a expression,you can add some parenthesis to maximize result.Example as following:
1 + 2 * 3 - 2
Its result is 7 in the example.But you can add a parenthesis like (1 + 2) * 3 - 2,then
the result is 7,and so it is the maximization of the example.You job is to find the maximization of a expression.
To simplify this problem,you can assume that expression contains only operator +, * and -, integers is none negative

上一篇:Microsoft : find the next biggest node in a BST
下一篇:Google : 句子分割
🔗
 楼主| wwwyhx 2011-5-31 13:15:27 | 只看该作者
全局:
觉得这题还是出的很好的,没人回答我晚上贴答案了哈
回复

使用道具 举报

🔗
 楼主| wwwyhx 2011-6-1 21:08:01 | 只看该作者
全局:
应为有负数的存在,一开始还以为是NP复杂,没想到DP可以记录最大值,同时一样可以记录最小值。对于加号来说,两边最大和最大,两边最小和最小。对于乘来说,两边最大最小的4种组合求乘积的最大最小值...

  1. struct OPERATION
  2. {
  3.         char cOpr;
  4.         int nLft;
  5.         int nRgt;

  6.         OPERATION(int l = 0, int r = 0, char op = 0)
  7.         {
  8.                 nLft = l;
  9.                 nRgt = r;
  10.                 cOpr = op;
  11.         }
  12. };

  13. int GetNoneNegNum(const char*& pIter)
  14. {
  15.         int nRet = -1;
  16.         while (*pIter == ' ') pIter++;

  17.         while (*pIter >= '0' && *pIter <= '9')
  18.         {
  19.                 if (nRet < 0) nRet = 0;
  20.                 nRet = nRet*10 + *pIter - '0';
  21.                 pIter++;
  22.         }

  23.         return nRet;
  24. }

  25. char GetOperator(const char*& pIter)
  26. {
  27.         while (*pIter == ' ') pIter++;

  28.         if (*pIter != '-' && *pIter != '+' && *pIter != '*')
  29.                 return 0;
  30.        
  31.         char cRet = *pIter++;
  32.         return cRet;
  33. }

  34. bool Parse(const char* szString, vector<OPERATION>& vec, int nLft)
  35. {
  36.         if (NULL == szString) return false;

  37.         if (vec.empty())
  38.                 nLft = GetNoneNegNum(szString);
  39.         if (nLft < 0) return false;

  40.         while (*szString == ' ') szString++;
  41.         if ('\0' == *szString) return true;

  42.         int cOpr = GetOperator(szString);
  43.         if (0 == cOpr) return false;

  44.         int nRgt = GetNoneNegNum(szString);
  45.         if (nRgt < 0) return false;

  46.         vec.push_back(OPERATION(nLft, nRgt, cOpr));

  47.         return Parse(szString, vec, nRgt);
  48. }

  49. int Calc(int l, char cOpr, int r)
  50. {
  51.         if ('+' == cOpr) return l+r;
  52.         if ('-' == cOpr) return l-r;
  53.         if ('*' == cOpr) return l*r;
  54.         return 0;
  55. }

  56. bool GetMaxResult(const char* szString, int& res)
  57. {
  58.         vector<OPERATION> recs;
  59.         if (!Parse(szString, recs, 0))
  60.                 return false;

  61.         struct RECORD
  62.         {
  63.                 int nMax;
  64.                 int nMin;
  65.         };

  66.         int nSize = recs.size();
  67.         RECORD** pRec = new RECORD*[nSize];
  68.         for (int i = 0; i < nSize; i++)
  69.                 pRec[i] = new RECORD[nSize];

  70.         for (int i = 0; i < nSize; i++)
  71.                 pRec[i][i].nMax = pRec[i][i].nMin = Calc(recs[i].nLft, recs[i].cOpr, recs[i].nRgt);

  72.         for (int i = 1; i < nSize; i++)
  73.                 for (int j = 0; j + i < nSize; j++)
  74.                 {
  75.                         int nMax = 0;
  76.                         int nMin = 0;

  77.                         for (int k = 0; k <= i; k++)
  78.                         {
  79.                                 int nLMax, nLMin, nRMax, nRMin;
  80.                                 if (0 == k)
  81.                                         nLMin = nLMax = recs[j].nLft;
  82.                                 else
  83.                                 {
  84.                                         nLMax = pRec[j][j+k-1].nMax;
  85.                                         nLMin = pRec[j][j+k-1].nMin;
  86.                                 }

  87.                                 if (i == k)
  88.                                         nRMin = nRMax = recs[j+i].nRgt;
  89.                                 else
  90.                                 {
  91.                                         nRMax = pRec[j+k+1][j+i].nMax;
  92.                                         nRMin = pRec[j+k+1][j+i].nMin;
  93.                                 }

  94.                                 int nTmpMax = 0;
  95.                                 int nTmpMin = 0;
  96.                                 char cOp = recs[j+k].cOpr;

  97.                                 if ('+' == cOp)
  98.                                 {
  99.                                         nTmpMax = nLMax + nRMax;
  100.                                         nTmpMin = nLMin + nRMin;
  101.                                 }

  102.                                 if ('-' == cOp)
  103.                                 {
  104.                                         nTmpMax = nLMax - nRMin;
  105.                                         nTmpMin = nRMin + nLMax;
  106.                                 }

  107.                                 if ('*' == cOp)
  108.                                 {
  109.                                         nTmpMax = max(max(nLMax*nRMax, nLMax*nRMin), max(nLMin*nRMin, nLMin*nRMax));
  110.                                         nTmpMin = min(min(nLMax*nRMax, nLMax*nRMin), min(nLMin*nRMin, nLMin*nRMax));
  111.                                 }

  112.                                 nMax = nMax > nTmpMax ? nMax : nTmpMax;
  113.                                 nMin = nMin < nTmpMin ? nMin : nTmpMin;
  114.                         }

  115.                         pRec[j][j+i].nMax = nMax;
  116.                         pRec[j][j+i].nMin = nMin;
  117.                 }

  118.         res = pRec[0][nSize-1].nMax;
  119.         for (int i = 0; i < nSize; i++)
  120.                 delete []pRec[i];
  121.         delete []pRec;

  122.         return true;
  123. }

复制代码
回复

使用道具 举报

🔗
 楼主| wwwyhx 2011-6-5 10:45:36 | 只看该作者
全局:
思路是可以用DP,对每个字组合记录最大结果和最小结果
对于(...)(+/-/*)(...)来说,
+ : 最大的结果取两边最大值相加,最小的结果为两边最小值相加
- :  最大的结果为左边最大值减去右边最小值, 最小的结果相反
* : 最大最小的结果为[minL*minR, minL*LargR, LargL*minL, LargL*minR]的最大,最小值
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表