fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <math.h>
  4. using std::cin;
  5. using std::endl;
  6. using std::cout;
  7. using std::string;
  8. int granice(string,int);
  9. string odwroc(string);
  10. string usun(string);
  11. int main()
  12. {
  13. int d;
  14. cin>>d;
  15. while(d--)
  16. {
  17. int ile;
  18. string napis;
  19. cin>>ile>>napis;
  20. napis=odwroc(napis);
  21. napis=usun(napis);
  22. int wynik=granice(napis,ile);
  23. cout<<wynik<<endl;
  24. }
  25. return 0;
  26. }
  27. string odwroc(string napis)
  28. {
  29. string wynik=napis;
  30. for(unsigned int i=0; i<napis.length(); i++)
  31. {
  32. wynik[i]=napis[napis.length()-1-i];
  33. }
  34. return wynik;
  35. }
  36. int granice(string napis,int ile)
  37. {
  38. int p=1,q=ile;
  39. for(unsigned int i=0; i<napis.length(); i++)
  40. {
  41. if(napis[i]=='A')
  42. q=(p+q)/2;
  43. else
  44. p=(p+q)/2+1;
  45. }
  46. return q;
  47. }
  48. string usun(string napis)
  49. {
  50. string wynik="";
  51. while(napis.length()>0)
  52. {
  53. string kopia="";
  54. if(napis[0]=='B')
  55. {
  56. wynik+=napis[0];
  57. napis=napis.erase(0,1);
  58. if(napis.length()>1)
  59. {
  60. int ile_razy=log2(napis.length()+2)-1;
  61. for(int j=0; j<ile_razy; j++)
  62. {
  63. napis=napis.erase(pow(2,j+1)-1,pow(2,j));
  64. }
  65. }
  66. }
  67. else
  68. {
  69. wynik+=napis[0];
  70. napis=napis.erase(0,1);
  71. if(napis.length()>1)
  72. {
  73. int ile_razy=log2(napis.length()+2)-1;
  74. for(int j=0; j<ile_razy; j++)
  75. {
  76. napis=napis.erase(pow(2,j)-1,pow(2,j));
  77. }
  78. }
  79. }
  80. }
  81.  
  82.  
  83. return wynik;
  84. }
  85.  
Success #stdin #stdout 0s 4472KB
stdin
3
8
ABAAABB
16
ABAAAABBAAABABB
32
ABAABBBAAABABABBABAAAAAAAAAABAA
stdout
7
16
10