fork download
  1. #include <bits/stdc++.h>
  2.  
  3. #define ll long long
  4. #define el cout << '\n'
  5.  
  6. using namespace std;
  7.  
  8. const int maxn = 1e5;
  9. const string AP = "0123456789ABCDEF";
  10.  
  11. int n, p[maxn + 10];
  12. string s;
  13.  
  14. int main()
  15. {
  16. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  17. if (fopen("REKMP.INP", "r"))
  18. {
  19. freopen("REKMP.INP", "r", stdin);
  20. freopen("REKMP.OUT", "w", stdout);
  21. }
  22.  
  23. cin >> n;
  24. for (int i = 1; i <= n; i++)
  25. {
  26. cin >> p[i];
  27. }
  28. string s = " 0";
  29. if (p[0])
  30. return cout << -1, 0;
  31. for (int i = 2; i <= n; i++)
  32. {
  33. // if (p[i])
  34. // {
  35. // if (p[i] != p[i - 1] + 1)
  36. // return cout << -1, 0;
  37. // s += s[p[i]];
  38. // }
  39. // else
  40. // {
  41. bool flag = 0;
  42. for (char c : AP)
  43. {
  44. int j = p[i - 1];
  45. while (j && s[j + 1] != c)
  46. j = p[j];
  47. if ((p[i] && s[j + 1] == c && p[i] == j + 1) || (!p[i] && s[j + 1] != c))
  48. {
  49. s += c;
  50. flag = 1;
  51. break;
  52. }
  53. // continue;
  54. // if (j == 0)
  55. // {
  56. // s += c;
  57. // flag = 1;
  58. // break;
  59. // }
  60. }
  61. if (!flag)
  62. return cout << -1, 0;
  63. // }
  64. }
  65. for (int i = 1; i < s.size(); i++)
  66. cout << s[i];
  67. }
  68.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty