Advertisement
Guest User

Untitled

a guest
Apr 13th, 2025
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int t; // No Of Test Cases
  6. cin>>t;
  7. while(t--){
  8. int n; // Lenght Of String entered in input
  9. cin>>n;
  10. string S;
  11. cin>>S; // the Original String
  12. int count_0=0; // Created a counter for counting no of zeroes
  13. int count_1=0; // Created a counter for counting no of ones
  14. int n_of_strings=1; // No Of Strings Which are possible, 1 because original string is also counted
  15. vector <string> Dummy; // a vector to store newly generated strings
  16. Dummy.push_back(S); // Stored Original String
  17. int j=0;
  18. while(j<Dummy.size()){
  19. string y=Dummy[j]; // Started with my original string
  20. count_0=0;
  21. count_1=0;
  22. for(int i=0;i<n;i++){
  23. // Counting No of zeroes and 1s
  24. if(Dummy[j][i]=='0'){
  25. count_0+=1;
  26. }
  27. else if(Dummy[j][i]=='1'){
  28. count_1+=1;
  29. }
  30. // if both count are equal then flip also there count shouldnt equal to 0
  31. if (count_0==count_1 && count_1!=0){
  32. for(int k=0;k<count_0*2;k++){ // *2 because number of zeroes and no of 1s both have to be flipped
  33. if (y[k]=='0'){ // 0 to 1
  34. y[k]='1';
  35. }
  36. else if(y[k]=='1'){ 0 //1 to 0
  37. y[k]='0';
  38. }
  39. }
  40. bool exists = find(Dummy.begin(), Dummy.end(), y) != Dummy.end(); // checking if the generated string exists in vector or is it a new one
  41. if (exists){
  42. continue;
  43. }
  44. else{
  45. Dummy.push_back(y);
  46. n_of_strings+=1;
  47. i=-1;
  48. count_0=0;
  49. count_1=0;
  50. }
  51. }
  52. }
  53. j++;
  54. }
  55. cout<<n_of_strings<<endl;
  56. }
  57.  
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement