Advertisement
a53

circular1

a53
Apr 10th, 2022
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. /// Boca Alina Gabriela - Colegiul National de Informatica Tudor Vianu Bucuresti
  2.  
  3. #include <fstream>
  4. #include<cmath>
  5. using namespace std;
  6. ifstream cin("circular.in");
  7. ofstream cout("circular.out");
  8. int a[26][26],v[26];
  9. long long circular(string s)
  10. {
  11. int cost=0,n;
  12. char j='A';
  13. n=s.size();
  14. for(int i=0; i<n; i++)
  15. {
  16. if(j<s[i])
  17. {
  18. if(s[i]-j<'Z'-s[i]+j-64)
  19. cost+=s[i]-j;
  20. else
  21. cost+='Z'-s[i]+j-64;
  22. }
  23. else
  24. {
  25. if(j-s[i]<'Z'-j+s[i]-64)
  26. cost+=j-s[i];
  27. else
  28. cost+='Z'-j+s[i]-64;
  29. }
  30.  
  31. j=s[i];
  32. }
  33. return cost;
  34.  
  35. }
  36. int main()
  37. {
  38. int c;
  39. cin>>c;
  40. string p, t,rez;
  41. cin>>p>>t;
  42. if(c==1)
  43. {
  44. cout<<circular(p);
  45. }
  46. else
  47. {
  48. int i,j,k=0,y=1;
  49. string alf="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  50. for(i=0; i<26; i++)
  51. {
  52. if(alf[i]==t[k])
  53. {
  54. v[k]=i;
  55. k++;
  56. }
  57. for(j=0; j<26; j++)
  58. if(i<j)
  59. {
  60. a[i][j]=min(abs(i-j), abs(26-j+i));
  61. }
  62. else
  63. {
  64. a[i][j]=min(abs(i-j), abs(26-i+j));
  65. }
  66. }
  67.  
  68. int nrtotal=1,costtotal=0,q;
  69. int x;
  70. for(j=0; j<alf.size(); j++)
  71. if(p[0]==alf[j])
  72. {
  73. k=j;
  74. break;
  75. }
  76. costtotal=min(k,26-k);
  77. rez.push_back(p[0]);
  78. for(i=1; i<p.size(); i++)
  79. {
  80. for(j=0; j<alf.size(); j++)
  81. if(p[i]==alf[j])
  82. {
  83. q=j;
  84. break;
  85. }
  86. int mincost=60,nr=0;
  87. for(j=0; j<t.size(); j++)
  88. {
  89. if (mincost>a[k][v[j]]+a[q][v[j]])
  90. {
  91. mincost=a[k][v[j]]+a[q][v[j]];
  92. nr=1;
  93. x=v[j];
  94.  
  95. }
  96. else if(a[k][v[j]]+a[q][v[j]]==mincost)
  97. {
  98. nr++;
  99. if(x>v[j])
  100. {
  101. x=v[j];
  102. }
  103. }
  104. }
  105. rez.push_back(alf[x]);
  106. nrtotal*=(nr%666013);
  107. nrtotal=nrtotal%666013;
  108. costtotal+=mincost;
  109. rez.push_back(p[i]);
  110. k=q;
  111. }
  112. cout<<costtotal<<endl<<nrtotal<<endl;
  113. cout<<rez;
  114. }
  115. return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement