Advertisement
a53

text2

a53
Dec 25th, 2021
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. //Emanuela Cerchez - 100 puncte
  2. #include <fstream>
  3. #include <cstring>
  4. #define InFile "text.in"
  5. #define OutFile "text.out"
  6. #define abs(x) ((x)>0?(x):(-(x)))
  7. #define LgMax 210
  8. #define Inf 2000000000
  9. #define R 1000003
  10. using namespace std;
  11.  
  12. char s[LgMax], v[]="aeiouy";
  13. int n, lg, cine;
  14. int poz[LgMax];
  15. //poz[i]=pozitia celei mai apropiate vocale >=i
  16.  
  17. long int nr[LgMax][2];
  18. //nr[i][k]=numarul de posibilitati de a construi k cuvinte incepand cu pozitia i
  19.  
  20. long int cost[LgMax][2];
  21. unsigned char urm[LgMax][LgMax];
  22.  
  23. void citire ();
  24. void rezolvare ();
  25. void afisare ();
  26. void det_poz();
  27. void numarare();
  28. void armonie();
  29.  
  30. int main ()
  31. {
  32. int i, j, k;
  33. ofstream fout (OutFile);
  34. citire ();
  35. det_poz();
  36. numarare ();
  37.  
  38. fout<<nr[0][cine]<<'\n';
  39.  
  40. armonie();
  41. fout<<cost[0][cine]<<'\n';
  42.  
  43. for (i=0, k=n; k>0; k--)
  44. { for (j=i; j<urm[i][k]; j++) fout<<s[j];
  45. i=urm[i][k];
  46. if (k>1) fout<<' ';
  47. }
  48. fout<<'\n';
  49. fout.close ();
  50. return 0;
  51. }
  52.  
  53. void citire ()
  54. {
  55. ifstream fin (InFile);
  56. fin.getline(s, LgMax);
  57. lg=strlen(s);
  58. fin>>n;
  59. fin.close ();
  60. }
  61.  
  62. void det_poz()
  63. {
  64. int i, last=-1;
  65. for (i=lg-1; i>=0; i--)
  66. {if (strchr(v,s[i]))last=i;
  67. poz[i]=last;}
  68. }
  69.  
  70. void numarare ()
  71. {
  72. int i, k, t, ok;
  73. long int sum;
  74. cine=0;
  75. for (i=0; i<lg; i++)
  76. if (poz[i]!=-1 && lg-i<=20) nr[i][0]=1;
  77. else
  78. nr[i][0]=-1;
  79.  
  80. for (k=2; k<=n; k++)
  81. {
  82. cine=1-cine;
  83. for (i=lg-1; i>=0; i--)
  84. {
  85. nr[i][cine]=-1;
  86. sum=0; ok=0;
  87. for (t=1; t<=20 && i+t-1<lg; t++)
  88. {if (poz[i]==-1 || i+t-1 <poz[i]) continue;
  89. if (nr[i+t][1-cine]!=-1)
  90. {ok=1; sum=(sum+nr[i+t][1-cine])%R;}
  91. }
  92. if (ok) nr[i][cine]=sum;
  93. }
  94. }
  95. }
  96.  
  97.  
  98. void armonie ()
  99. {
  100. int i, k, t, tmin;
  101. long int min;
  102.  
  103. cine=0;
  104. for (i=0; i<lg; i++)
  105. {
  106. if (poz[i]!=-1 && lg-i<=20) {cost[i][0]=(lg-i)*(lg-i); urm[i][1]=lg;}
  107. else
  108. {cost[i][0]=Inf; urm[i][1]=0;}
  109. }
  110. for (i=0; i<=lg; i++) urm[lg][i]=0;
  111. cost[lg][0]=cost[lg][1]=Inf;
  112.  
  113. for (k=2; k<=n; k++)
  114. {
  115. cine=1-cine;
  116. for (i=lg-1; i>=0; i--)
  117. {
  118. cost[i][cine]=Inf; urm[i][k]=0;
  119. min=Inf; tmin=-1;
  120.  
  121. for (t=1; t<=20 && i+t-1<lg; t++)
  122. {if (poz[i]==-1 || i+t-1 <poz[i]) continue;
  123. if (cost[i+t][1-cine]==Inf) continue;
  124.  
  125. if (cost[i+t][1-cine]+t*t<min)
  126. {
  127. min=cost[i+t][1-cine]+t*t;
  128. tmin=t;
  129. }
  130. }
  131. if (tmin!=-1) {cost[i][cine]=min; urm[i][k]=i+tmin;}
  132. }
  133.  
  134. }
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement