├── CODE.py ├── Problem Explanation.txt └── ALGORITHM.txt /CODE.py: -------------------------------------------------------------------------------- 1 | for i in range(int(input())): 2 | st=input() 3 | A=set() 4 | for i in range(len(st)): 5 | ct=0 6 | even=0 7 | odd=0 8 | for j in range(i,len(st)): 9 | if(st[j]=="1"): 10 | ct+=1 11 | else: 12 | if(ct%2==0): 13 | even+=1 14 | else: 15 | odd+=1 16 | A.add((j-i+1,even,odd)) 17 | print(len(A)) 18 | A.clear() -------------------------------------------------------------------------------- /Problem Explanation.txt: -------------------------------------------------------------------------------- 1 | This problem involves finding the number of equivalence classes among the substrings of a binary string. Two strings A and B are considered equivalent if they have the same lengths and A can be transformed into B by performing zero or more operations of reversing a substring that contains '1' an even number of times. 2 | 3 | To solve this problem, we can use the following approach: 4 | 5 | 1. Read the integer T from input, which represents the number of test cases. 6 | 7 | 2. Iterate T times to process each test case. 8 | 9 | 3. For each test case, read the binary string S from input. 10 | 11 | 4. Initialize a set C to store the equivalence classes of substrings. 12 | 13 | 5. Iterate over all possible substrings of S. For each substring R, do the following steps: 14 | 15 | a. Initialize a counter variable cnt to count the number of '1' characters in R. 16 | 17 | b. If cnt is even, add the substring R to the set C. 18 | 19 | 6. Print the size of the set C, which represents the number of equivalence classes among the substrings of S. 20 | 21 | 22 | -------------------------------------------------------------------------------- /ALGORITHM.txt: -------------------------------------------------------------------------------- 1 | Let's go through the algorithm of the code step by step: 2 | 3 | 1. Read the number of test cases using `int(input())` and iterate `i` over the range of test cases. 4 | 5 | 2. Read the binary string `st` for the current test case. 6 | 7 | 3. Initialize an empty set `A` to store the equivalence classes. 8 | 9 | 4. Iterate `i` over the range of the length of the string `st`. 10 | 11 | 5. Initialize variables `ct`, `even`, and `odd` to 0. These variables will be used to count the number of '1' characters, even '1' counts, and odd '1' counts in each substring. 12 | 13 | 6. Iterate `j` over the range from `i` to the length of the string `st`. This represents the ending index of the current substring. 14 | 15 | 7. Check if the character at index `j` in the string `st` is '1'. If it is, increment the `ct` variable to count the number of '1' characters. 16 | 17 | 8. If the character is not '1', check if the count `ct` is even. If it is, increment the `even` counter; otherwise, increment the `odd` counter. 18 | 19 | 9. Add the tuple `(j-i+1, even, odd)` to the set `A`. The tuple represents the length of the substring (`j-i+1`), the count of even '1' characters (`even`), and the count of odd '1' characters (`odd`). 20 | 21 | 10. After iterating over all possible substrings, print the length of the set `A`, which represents the number of equivalence classes among the substrings of the given string `st`. 22 | 23 | 11. Clear the set `A` to prepare for the next test case. 24 | --------------------------------------------------------------------------------