📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
查看: 3902| 回复: 6
跳转到指定楼层
上一主题 下一主题
收起左侧

Yahoo : 转换到回文字符串

全局:

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

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

x
given a word,convert it into a pallindrome with minimum addition of letters to it.letters can be added anywhere in the word.for eg if yahoo is given result shud be yahoohay.give a optimize soln

上一篇:Amazon : find BST of max size in a binary tree
下一篇:Yahoo : 找出最长0,1对数相同的子串
全局:
回复 1# wwwyhx

#include <iostream>
#include <string>
using namespace std;

#define N 80


bool isHuiWen(string str)
{
int i;
for(i=0;i<str.length()-1-i;i++)
  if(str[i]!=str[str.length()-1-i])
   return 0;
return 1;
}

int getMin(string src,string &dest)
{
if(isHuiWen(src))
{
  dest=src;
  return 0;
}

char p[N];
int top=0;

string temp=src;
string tempL,tempR;
int m1,m2;
   
while(temp[0]==temp[temp.length()-1])
{
  p[top++]=temp[0];  
  temp=temp.substr(1,temp.length()-2);
}

m1=getMin(temp[temp.length()-1]+temp,tempL);
m2=getMin(temp+temp[0],tempR);
if(m1<m2) dest=tempL;
else dest=tempR;
  
while(top>0)
{
  top--;
  dest=p[top]+dest;
  dest+=p[top];
}

return dest.length()-src.length();
}

void main(void)
{
string str,result;
cin>>str;
getMin(str,result);
cout<<result<<endl;
}
回复

使用道具 举报

🔗
 楼主| wwwyhx 2011-5-4 20:07:30 | 只看该作者
全局:
回复  wwwyhx

#include  
#include
using namespace std;

#define N 80


bool isHuiWen(string str)
{
int i;
for(i=0;i>str;
getMin(str,result);
cout
kidd_yq@163.com 发表于 2011-5-4 19:16



   

说下思路吧
回复

使用道具 举报

全局:
本帖最后由 kidd_yq@163.com 于 2011-5-4 20:47 编辑

回复 3# wwwyhx


检测串的头和尾,如果对称则入栈,截取除头尾之外的子串,直到找出最短的非对称的子串,栈中保存了对称的部分;
接下来有两种匹配对称的方法(在串头插入和串尾相同的字符/在串尾插入和串头相同的字符),直到某一次插入后满足回文条件;再将栈中对称的部分插入到串的头和尾;
递归这个过程,选取增加字符数少的子串返回,直到最初输入的串满足回文的标准。
回复

使用道具 举报

🔗
 楼主| wwwyhx 2011-5-4 23:52:16 | 只看该作者
全局:
我的一个解法:
设这个字符串为str1,获取逆置字符串str2,获取str1和str2的Longest sub sequence ==> LCS

做一个str1,str2,str3的3路归并,输出到存放结果的数组
回复

使用道具 举报

🔗
 楼主| wwwyhx 2011-5-4 23:53:05 | 只看该作者
全局:
回复 5# wwwyhx

  1. char* CalcLCS(const char* szStr1, const char* szStr2, char pOut[])
  2. {
  3.         assert(szStr2 && szStr1 && pOut);
  4.         int nLen1 = strlen(szStr1);
  5.         int nLen2 = strlen(szStr2);

  6.         if (nLen1 == 0 || nLen2 == 0)
  7.         {
  8.                 pOut[0] = 0;
  9.                 return pOut;
  10.         }

  11.         int** pDP = new int*[nLen1];
  12.         for (int i = 0; i < nLen1; i++)
  13.                 pDP[i] = new int[nLen2];

  14.         //initialize
  15.         bool bMatch = false;
  16.         for (int i = 0; i < nLen2; i++)
  17.         {
  18.                 if (bMatch || szStr2[i] == szStr1[0])
  19.                 {
  20.                         bMatch = true;
  21.                         pDP[0][i] = 1;
  22.                 }
  23.         }

  24.         bMatch = false;
  25.         for (int i = 0; i < nLen1; i++)
  26.         {
  27.                 if (bMatch || szStr1[i] == szStr2[0])
  28.                 {
  29.                         bMatch = true;
  30.                         pDP[i][0] = 1;
  31.                 }
  32.         }

  33.         //Calculate lcs length
  34.         for (int i = 1; i < nLen1; i++)
  35.         {
  36.                 for (int j = 1; j < nLen2; j++)
  37.                 {
  38.                         if (szStr1[i] == szStr2[j])
  39.                                 pDP[i][j] = pDP[i-1][j-1] + 1;
  40.                         else
  41.                                 pDP[i][j] = pDP[i-1][j] > pDP[i][j-1] ? pDP[i-1][j] : pDP[i][j-1];
  42.                 }
  43.         }

  44.         //put string to pOut
  45.         int nLCSLen = pDP[nLen1-1][nLen2-1];
  46.         int nIter = nLCSLen;
  47.         pOut[nIter--] = 0;

  48.         int x = nLen1-1;
  49.         int y = nLen2-1;
  50.         while (nIter >= 0)
  51.         {
  52.                 if (0 == x)
  53.                 {
  54.                         pOut[0] = szStr1[0];
  55.                         break;
  56.                 }

  57.                 if (0 == y)
  58.                 {
  59.                         pOut[0] = szStr2[0];
  60.                         break;
  61.                 }

  62.                 if (pDP[x][y] == pDP[x-1][y-1]+1 && szStr1[x] == szStr2[y])
  63.                 {
  64.                         pOut[nIter--] = szStr1[x];
  65.                         x--,y--;
  66.                 }
  67.                 else if (pDP[x-1][y] == pDP[x][y])
  68.                         x--;
  69.                 else y--;
  70.         }

  71.         for (int i = 0; i < nLen1; i++)
  72.                 delete []pDP[i];
  73.         delete []pDP;

  74.         return pOut;
  75. }

  76. char* TurnToPallindrome(const char* szStr, char pOut[])
  77. {
  78.         int nLen = strlen(szStr);
  79.         char* pLCS = new char[nLen+1];
  80.         char* pReverse = new char[nLen+1];

  81.         pReverse[nLen] = 0;
  82.         int nIter = 0;
  83.         for (int i = nLen-1; i >= 0; i--)
  84.                 pReverse[nIter++] = szStr[i];

  85.         CalcLCS(szStr, pReverse, pLCS);

  86.         //three way merge
  87.         const char* p1 = szStr;
  88.         const char* p2 = pLCS;
  89.         const char* p3 = pReverse;
  90.         char* pWrite = pOut;
  91.        
  92.         while (*p2 != 0)
  93.         {
  94.                 if (*p1 != *p2)
  95.                 {
  96.                         *pWrite++ = *p1++;
  97.                         continue;
  98.                 }

  99.                 if (*p3 != *p2)
  100.                 {
  101.                         *pWrite++ = *p3++;
  102.                         continue;
  103.                 }

  104.                 *pWrite++ = *p2++;
  105.                 p1++,p3++;
  106.         }
  107.        
  108.         while (*p1 != 0) *pWrite++ = *p1++;
  109.         while (*p3 != 0) *pWrite++ = *p3++;

  110.         *pWrite = 0;

  111.         delete []pLCS;
  112.         delete []pReverse;

  113.         return pOut;
  114. }
复制代码
回复

使用道具 举报

🔗
darksteel 2011-5-5 01:11:23 | 只看该作者
全局:
本帖最后由 darksteel 于 2011-5-5 01:13 编辑

上面两种办法应该都是可行的。递归的办法写起来简练,但如果不保存点信息的话,效率很低,最坏是指数级的。空间最坏是O(n^2)。lz的方法其实有点dp的意思,时间空间都是O(n^2)。我一直在考虑的是有没有复杂度更低的办法,因为字符串可能很长,字符串算法一般O(n^2)都是很难接受的,不过我没想出来。。
但如果O(n^2)是可以接受的话,倒是有比较简练的DP方法,其实这个问题如果问最少add多少字母就是典型的DP问题。要输出结果的话,根据DP中间值把最优解恢复出来就行,代码如下,欢迎指出bug。
  1. #define MAXL 1010
  2. char s[MAXL];
  3. int dp[MAXL][MAXL];
  4. int getPath(int p, int l, char *ans)
  5. {
  6.         if(l < 2) {
  7.                 if(l) *ans = s[p];
  8.                 return l;
  9.         }
  10.         if(s[p] == s[p+l-1]) {
  11.                 *ans = s[p];
  12.                 return 1+getPath(p+1, l-2, ans+1);
  13.         } else if(dp[p][l] == 1+dp[p+1][l-1]) {
  14.                 *ans = s[p];
  15.                 return 1+getPath(p+1, l-1, ans+1);
  16.         } else {
  17.                 *ans = s[p+l-1];
  18.                 return 1+getPath(p, l-1, ans+1);
  19.         }
  20. }
  21. int main()
  22. {
  23.         char ans[MAXL*2];
  24.         cin >> s;
  25.         int i, j, l = strlen(s);
  26.         for(i = 2; i <= l; i++)
  27.                 for(j = 0; j <= l-i; j++) {
  28.                         if(s[j] == s[j+i-1])
  29.                                 dp[j][i] = dp[j+1][i-2];
  30.                         else
  31.                                 dp[j][i] = 1+min(dp[j+1][i-1], dp[j][i-1]);
  32.                 }
  33.         cout << dp[0][l] << endl;
  34.         int len = getPath(0, l, ans);
  35.         ans[l+dp[0][l]] = 0;
  36.         for(i = l+dp[0][l]-1; i >= len; i--)
  37.                 ans[i] = ans[l+dp[0][l]-i-1];
  38.         cout << ans << endl;
  39.         return 0;
  40. }
复制代码
回复

使用道具 举报

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

本版积分规则

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