-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary substitution solution
90 lines (79 loc) · 3.06 KB
/
binary substitution solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
Problem
Chef has a binary string SS. He can replace any occurrence of -
0101 with aa
1010 with bb
010010 with abab
101101 with baba
While doing these operations, Chef noticed that he can end up with different strings depending upon the order of application of the operations. Given the final string containing only aa and bb, Chef wants to know the number of possible strings he might have began with.
As the number of initial strings can be large, print the result modulo 998244353998244353.
Input Format
The first line of input will contain a single integer TT, denoting the number of test cases.
Each test case consists of a single line of input denoting SS, the final string obtained after applying the operations.
Output Format
For each test case, output the number of original strings that can result in the final string mod 998244353998244353.
Constraints
1 \leq T \leq 5\cdot 10^41≤T≤5⋅10
4
1 \leq |S| \leq 10^51≤∣S∣≤10
5
The sum of |S|∣S∣ over all test cases won't exceed 5\cdot 10^55⋅10
5
.
SS consists of aa and bb only.
Sample 1:
Input
Output
3
ab
aa
abb
2
1
2
Explanation:
Test case 11: The binary strings that can result in the string abab are 01100110 and 010010.
01100110: Replace the first two characters 0101 with aa and last two characters 1010 with bb. Thus, we get abab.
010010: Replace the characters 010010 with abab.
Test case 22: The only binary string that can result in the string aaaa is 01010101. In 01010101, we replace the first two characters with aa and last two characters with aa as well.
Test case 33: The binary strings that can result in the string abbabb are 011010011010 and 0101001010.
011010011010: Replace the first two characters 0101 with aa, next two characters 1010 with bb, and last two characters 1010 with bb. Thus, we get abbabb.
0101001010: Replace the characters 010010 with abab and the characters 1010 with bb.
answer:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
long long int func(int index, string& s, vector<int>& dp, map<string,string>& hardcoded, string curr, set<string>& disset, long long int mod){
if(index<0){
auto it = disset.find(curr);
if(it==disset.end()){
disset.insert(curr);
return 1;
}
return 0;
}
if(dp[index] != -1) return dp[index];
long long int takingone=func(index-1, s, dp, hardcoded, hardcoded[s.substr(index, 1)+curr], disset, mod);
long long int takingtwo = 0;
if(index-1 >= 0 && hardcoded.find(s.substr(index-1, 2)) != hardcoded.end())
takingtwo = func(index-2, s, dp, hardcoded, hardcoded[s.substr(index-1, 2)]+curr, disset, mod);
return dp[index] = (takingone+takingtwo) % 998244353;
}
long long int solve(){
string s;
cin>>s;
long long int n = s.length();
vector<int> dp(n, -1);
map<string, string> hardcoded {{"a", "01"}, {"b", "10"}, {"ab", "010"}, {"ba", "101"}};
set<string> disset;
cout<< func(n-1, s, dp, hardcoded, "", disset, 998244353) <<endl;
return 0;
}
int main(){
long long int t;
cin >> t;
while(t--)
{
solve();
}
return 0;
}