Description

Those who have see the film of "Kong Fu Panda" must be impressive when Po opens the dragon scroll, because nothing was recorded on it! Po was surprising at the situation; Tai Lung and Master Shifu were also surprising since no body believes that the mystic dragon scroll is just a blank paper. After Tai Lung was defeated, Po found Master Wugui’s Diary and know that the dragon scroll recorded some messages long long years ago, but these messages were blurred due to abrasions. Master Wugui has copied these blurred messages and he wants someone to recover these messages. The messages has a specific length, and each position could be digit, '?' or ','. It is known that each message recorded some strictly increasing positive integers (without leading zeroes) separated by commas and Po is asked to recover these numbers.

Input

There are multiple test cases. Each test case contains string in a line represented. The length of the string will not exceed \(500\). The format is shown in the sample input.

Output

If the message has no appropriate solution, print "impossible", else print the decrypted message. If there exists multiple solutions, output the one whose first number is the smallest; if there is a tie, output the one whose second number is the smallest; and so on.

Sample Input

?,10,?????????????????,16,??

?2?5??7?,??

???????????????????????????????,???

Sample Output

impossible

12,50,70,71

1,2,3,4,5,6,7,8,9,10,11,100,101,102

这个题目dp应该很好想,然后就是难得写。

我们用\(f_i\)表示从\(i\)这个位置开始的合法序列第一个数字最大是多少。然后很容易想到dp来求\(f_i\)。

\[f_i = maxconvert(i,j,j+2)
\]

这个\(maxconvert\)的意思是在\([i,j]\)中填数字,\(j+1\)上填',',构造出比\(f_{j+2}\)小最大的数字是多少。

然后这个\(maxconvert\)写起来有些日狗。

我们将\(S_i \sim S_j\)记作\(S\),将\(f_{j+2}\)记作\(pat\)。

我们抓住这一点——我们肯定是要找到最后大的\(id\),使得\(S_1 \sim S_{id-1} = pat_1 \sim pat_{id-1}\),然后\(S_{id} < pat_{id}\),且\(S_{id}\)要填满足的最大值。\(S_{id+1} \sim\)的'?'全部换成'9'。

之后有了\(f\),我们就可以判断有无解了。

但是还要输方案,我们可以采用贪心的思想,每次都填最小的。假设我们已经填好了\(1 \sim i\),要在\(i+2 \sim j\)中填上数字,我们只要保证填的数字小于\(f_{j+2}\)即可。我们可以构造一个\(minconvert(i,j,j+2)\)函数,表示在\([i,j]\)中填数字,使得填出来的数字比\(S_1 \sim S_j-1\)的数字中的最大值大的最小值是多少。当然要保证此数字小于\(f_{j+2}\)。然后这题就做完了。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cstring>
  5. using namespace std;
  6. const int maxn = 510;
  7. char S[maxn]; int N;
  8. struct Node
  9. {
  10. char s[maxn]; int len;
  11. friend inline bool operator <(const Node &a,const Node &b)
  12. {
  13. if (a.len != b.len) return a.len < b.len;
  14. for (int i = 0;i < a.len;++i) if (a.s[i] != b.s[i]) return a.s[i] < b.s[i];
  15. return 0;
  16. }
  17. inline Node &operator=(const Node &a)
  18. {
  19. if (this == &a) return *this;
  20. len = a.len;
  21. for (int i = 0;i < len;++i) s[i] = a.s[i]; s[len] = 0;
  22. return *this;
  23. }
  24. inline Node():len(0) { s[len] = 0; }
  25. }f[maxn],now;
  26. inline void maxconvert(int x,int y,const Node &z)
  27. {
  28. if (y+2 < N&&y-x+1 > z.len) return;//位数多了,gg
  29. Node tmp;
  30. if (y+2 >= N||y-x+1 < z.len)
  31. {
  32. for (int i = x;i <= y;++i) tmp.s[tmp.len++] = (S[i] == '?'?'9':S[i]);
  33. if (tmp.s[0] != '0') f[x] = tmp;
  34. return;
  35. }//位数少了,补9
  36. int id = -1; //位数一样
  37. for (int i = x;i <= y;++i)
  38. {
  39. if (S[i] == z.s[i-x]) continue;
  40. if (S[i] != '?'&&S[i] > z.s[i-x]) { if (id != -1) break; return; }
  41. if (S[i] == '?'&&z.s[i-x] == '0') continue;
  42. if (S[i] == '?'&&z.s[i-x] == '1'&&i == x) continue;
  43. id = i;
  44. if (S[i] != '?'&&S[i] < z.s[i-x]) break;
  45. }
  46. if (id == -1) return;
  47. for (int i = x;i < id;++i) tmp.s[tmp.len++] = z.s[i-x];
  48. if (S[id] == '?') tmp.s[tmp.len++] = z.s[id-x]-1;
  49. else tmp.s[tmp.len++] = S[id];
  50. for (int i = id+1;i <= y;++i) tmp.s[tmp.len++] = (S[i] == '?'?'9':S[i]);
  51. if (tmp.s[0] != '0') f[x] = tmp;
  52. }
  53. inline bool minconvert(int x,int y,const Node &z)
  54. {
  55. if (y-x+1 < now.len) return false;//位数多了,gg
  56. Node tmp;
  57. if (y-x+1 > now.len)
  58. {
  59. tmp.s[tmp.len++] = (S[x] == '?'?'1':S[x]);
  60. for (int i = x+1;i <= y;++i) tmp.s[tmp.len++] = (S[i] == '?'?'0':S[i]);
  61. if (tmp.s[0] != '0'&&(y+1 >= N||tmp < z))
  62. {
  63. for (int i = x;i <= y;++i) S[i] = tmp.s[i-x];
  64. now = tmp; return true;
  65. }
  66. return false;
  67. }
  68. int id = -1;
  69. for (int i = x;i <= y;++i)
  70. {
  71. if (now.s[i-x] == S[i]) continue;
  72. if (S[i] != '?'&&S[i] < now.s[i-x]) { if (id != -1) break; return false; }
  73. if (S[i] == '?'&&now.s[i-x] == '9') continue;
  74. id = i;
  75. if (S[i] != '?'&&S[i] > now.s[i-x]) break;
  76. }
  77. if (id == -1) return false;
  78. for (int i = x;i < id;++i) tmp.s[tmp.len++] = now.s[i-x];
  79. if (S[id] == '?') tmp.s[tmp.len++] = now.s[id-x]+1;
  80. else tmp.s[tmp.len++] = S[id];
  81. for (int i = id+1;i <= y;++i) tmp.s[tmp.len++] = (S[i] == '?'?'0':S[i]);
  82. if (tmp.s[0] != '0'&&(y+1 >= N||tmp < z))
  83. {
  84. for (int i = x;i <= y;++i) S[i] = tmp.s[i-x];
  85. now = tmp; return true;
  86. }
  87. return false;
  88. }
  89. int main()
  90. {
  91. freopen("3717.in","r",stdin);
  92. freopen("3717.out","w",stdout);
  93. while (scanf("%s",S) != EOF)
  94. {
  95. N = strlen(S);
  96. for (int i = 0;i < N+10;++i) f[i].len = 0;
  97. for (int i = N-1;i >= 0;--i)
  98. {
  99. if (S[i] == ',') continue;
  100. for (int j = i;j < N;++j)
  101. {
  102. if (j+1 >= N||S[j+1] == ','||(S[j+1] == '?'&&j+2 < N&&S[j+2] != ','))
  103. {
  104. maxconvert(i,j,f[j+2]);
  105. if (S[j+1] == ',') break;
  106. }
  107. }
  108. }
  109. now.len = 0; bool fnd = true;
  110. for (int i = 0;i < N;++i)
  111. {
  112. if (S[i] == ',') continue;
  113. bool succ = false;
  114. for (int j = i;j < N;++j)
  115. if (j+1 >= N||S[j+1] == ','||(S[j+1] == '?'&&j+2 < N&&S[j+2] != ','))
  116. {
  117. if (minconvert(i,j,f[j+2]))
  118. {
  119. if (S[j+1] == '?') S[j+1] = ',';
  120. succ = true; i = j; break;
  121. }
  122. if (S[j+1] == ',') break;
  123. }
  124. if (!succ) { fnd = false; break; }
  125. }
  126. if (!fnd) puts("impossible");
  127. else puts(S);
  128. }
  129. fclose(stdin); fclose(stdout);
  130. return 0;
  131. }

POJ3717 Decrypt the Dragon Scroll的更多相关文章

  1. 【构建Android缓存模块】(一)吐槽与原理分析

    http://my.oschina.net/ryanhoo/blog/93285 摘要:在我翻译的Google官方系列教程中,Bitmap系列由浅入深地介绍了如何正确的解码Bitmap,异步线程操作以 ...

  2. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  3. FC红白机游戏列表(维基百科)

    1055个fc游戏列表 日文名 中文译名 英文版名 发行日期 发行商 ドンキーコング 大金刚 Donkey Kong 1983年7月15日 任天堂 ドンキーコングJR. 大金刚Jr. Donkey K ...

  4. [转]Decrypt Any iOS Firmware on Mac, Windows, Linux

    source:http://www.ifans.com/forums/threads/decrypt-any-ios-firmware-on-mac-windows-linux.354206/ Dec ...

  5. how to use fiddler and wireshark to decrypt ssl

    原文地址: http://security14.blogspot.jp/2010/07/how-to-use-fiddler-and-wireshark-to.html Requirements2 C ...

  6. 【前端性能】高性能滚动 scroll 及页面渲染优化

    最近在研究页面渲染及web动画的性能问题,以及拜读<CSS SECRET>(CSS揭秘)这本大作. 本文主要想谈谈页面优化之滚动优化. 主要内容包括了为何需要优化滚动事件,滚动与页面渲染的 ...

  7. MUI开发APP,scroll组件,运用到区域滚动

    最近在开发APP的过程中,遇到一个问题,就是内容有一个固定的头部和底部.         头部就是我们常用的header了,底部的话,就放置一个button,用来提交页面数据或者进入下一个页面等,效果 ...

  8. 翻唱曲练习:龙珠改主题曲 【Dragon Soul】龙之魂

    首先这是个人翻唱曲: 这个是原版(燃): 伴奏:  翻唱合成为动漫AMV 出镜翻唱: 全民K歌链接: http://kg.qq.com/node/play?s=aYpbMWb6UwoU&g_f ...

  9. 完美解决,浏览器下拉显示网址问题 | 完美解决,使用原生 scroll 写下拉刷新

    在 web 开发过程中我们经常遇到,不想让用户下拉看到我的地址,也有时候在 div 中没有惯性滚动,就此也出了 iScroll 这种关于滚动条的框架,但是就为了一个体验去使用一个框架好像又不值得,今天 ...

随机推荐

  1. Linux自带mariadb卸载

    MySQL安装过程中报错: dpkg: regarding mysql-community-server_5.6.39-1debian9_i386.deb containing mysql-commu ...

  2. Shell学习——Shell分类:登录shell和非登陆shell 交互shell和非交互shell

    1.从两个不同维度来划分,是否交互式,是否登录 2.交互式shell和非交互式shell 交互式模式:在终端上执行,shell等待你的输入,并且立即执行你提交的命令.这种模式被称作交互式是因为shel ...

  3. ethereum(以太坊)(八)--Address

    pragma solidity ^0.4.0; contract Test{ address _owner; uint160 _c; constructor() public{ _owner = 0x ...

  4. POJ:1258-Agri-Net

    Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65322 Accepted: 27029 Descriptio ...

  5. [工具使用]xshell 中“快速命令集”的使用

    突然看到朋友的xshell比我多一个按钮,且一点,哈哈哈 ,实现了很炫酷的功能,耐不住好奇,问了一句,原来是快速命令集! 1.选择快速命令集(两种方法a&b) a:文件 > 属性 > ...

  6. CodeForces 778D Parquet Re-laying 构造

    题意: 有两个\(n \times m\)的矩阵\(A,B\),都是由\(1 \times 2\)的砖块铺成,代表初始状态和结束状态 有一种操作可以把两个砖块拼成的\(2 \times 2\)的矩形旋 ...

  7. Android Stadio 导入moudle 不显示

    Android Stadio 导入moudle 不显示,moudle 里面的java类也没有识别,只当是普通的txt文件. 后来,我发现,每个moudle 都有一个.iml 文件~ 然后我就随便翻翻配 ...

  8. MySQL之查询性能优化(四)

    优化特定类型的查询 COUNT()的作用 COUNT()是一个特殊函数,有两个非常不同的作用:它可以统计某个列值的数量,也可以统计行数.在统计列值时要求列值是非空的(不统计NULL). 如果在COUN ...

  9. JQuery方法总结

    JQuery方法总结 Dom: Attribute:(属性) $("p").addClass(css中定义的样式类型); 给某个元素添加样式 $("img"). ...

  10. 《1024伐木累》-小白篇之丽jie(结束篇)-总章节六

    往期回顾:  机缘巧合,月侠发现了老王和他心仪女孩儿的秘密,这让他倍感愤怒,一年以后,丽姐又在去往老王家的路上,这让月侠感到历史即将重新上演,他想拦住丽姐,可恰巧丽姐手机没电,失去了联系. 小序 有人 ...