Advertisement
Kali_prasad

no.of cuts required to get perfect string

Apr 10th, 2022
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. #pragma GCC optimize ("O3")
  2. #pragma GCC target ("sse4")
  3.  
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6.  
  7. typedef long long ll;
  8. typedef pair<int, int> pii;
  9. typedef pair<string,int> psi;
  10. typedef unordered_map<int,int> mii;
  11. typedef unordered_map<long long,long long> mll;
  12. typedef unordered_map<string,int> msi;
  13. typedef unordered_map<char,int> mci;
  14. typedef unordered_set<int> si;
  15. typedef unordered_set<long long> sll;
  16. typedef unordered_set<string> ss;
  17. typedef unordered_set<char> sc;
  18. typedef map<int,int> ormii;
  19. typedef map<long long,long long> ormll;
  20. typedef map<string,int> ormsi;
  21. typedef map<char,int> ormci;
  22. typedef set<int> orsi;
  23. typedef set<long long> orsll;
  24. typedef set<string> orss;
  25. typedef set<char> orsc;
  26. typedef vector<int> vi;
  27. typedef vector<string> vs;
  28. typedef vector<char> vc;
  29. typedef vector<ll> vll;
  30. typedef vector<vector<int>> vvi;
  31. typedef vector<vector<string>> vvs;
  32. typedef vector<vector<ll>> vvll;
  33.  
  34. #define FOR(i, a, b) for (auto i=a; i<=(b); i++)
  35. #define FORd(i,b,a) for (int i =b; i >= a; i--)
  36. #define sz(x) (int)(x).size()
  37. #define mp make_pair
  38. #define pb push_back
  39. #define fi first
  40. #define se second
  41. #define ins insert
  42.  
  43. const int MOD = 1000000007;
  44. //type functions here
  45.  
  46. int cuts(string s)
  47. {
  48. int n=sz(s);
  49. if(n==1) return 0;
  50. int flag=0;
  51. for(int i=1;i<n;i++)
  52. {
  53. if(s[i-1]!=s[i])
  54. {
  55. flag=1;
  56. break;
  57. }
  58. }
  59. if(flag==0) return 0;
  60. if(n%2==1) return -1;
  61. int left,right;
  62. left=cuts(s.substr(0,n/2));
  63. right=cuts(s.substr(n/2,n));
  64. int count;
  65. //cout<<left<<" "<<right<<endl;
  66. if(left==0||right==0)
  67. count=0;
  68. else if(left==-1&&right==-1)
  69. return -1;
  70. else if(left==-1||right==-1)
  71. count=max(left,right);
  72. else
  73. count=min(left,right);
  74.  
  75. return count+1;
  76. }
  77.  
  78. int main() {
  79. ios_base::sync_with_stdio(false);
  80. cin.tie(NULL);
  81.  
  82. int tc=1;
  83. cin>>tc;
  84. FOR(w,1,tc)
  85. {
  86. string s="bbbbbabbabba";
  87. cin>>s;
  88. int c=cuts(s);
  89. cout<<c<<endl;
  90. }
  91. return 0;
  92. }
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement