Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define int long long
- using namespace std;
- const int INF = 1e9;
- struct alah{
- int i, j;
- };
- bool equals(char a, char b){
- return a == '?' || b == '?' || a == b;
- }
- string update_str(string s){
- string res = "";
- for(int i = 0; i < s.size(); i++){
- if(s[i] == '?') res += 'a';
- else if(s[i] != '*') res += s[i];
- }
- return res;
- }
- bool check(string & s1, string & s2){
- vector<vector<int> > dp(s1.size() + 1, vector<int> (s2.size() + 1, INF));
- vector<int> pos1, pos2;
- for(int i = 0; i < s1.size(); i++){
- if(s1[i] == '*')pos1.push_back(i);
- }
- for(int j = 0; j < s2.size(); j++){
- if(s2[j] == '*')pos2.push_back(j);
- }
- int i = 0, j = 0;
- if(s1 == "*" || s2 == "*"){
- if(s1 == "*" && s2 != s1){
- s2 = update_str(s2);
- s1 = s2;
- cout << s2.size() << endl;
- return true;
- }
- if(s2 == "*" && s2 != s1){
- s1 = update_str(s1);
- s2 = s1;
- cout << s1.size();
- return true;
- }
- s1 = s2 = "";
- cout << 0 << endl;
- return true;
- }
- if(i >= s1.size() || j >= s2.size()){
- return false;
- }
- if(equals(s1[i], s2[j])){
- dp[i][j] = 1;
- }
- if(s1[i] == '*' || s2[j] == '*'){
- dp[i][j] = 1;
- }
- if(s1[i] == '*' && s2[j] == '*'){
- dp[i][j] == 0;
- }
- queue<alah> q;
- q.push({i, j});
- while(!q.empty()){
- int i = q.front().i,
- j = q.front().j;
- q.pop();
- if(i + 1 > s1.size() || j + 1 > s2.size()) continue;
- int len = dp[i][j];
- int i1 = i + 1, j1 = j + 1;
- if(s1[i1] == '*'){
- dp[i1][j1] = len + 1;
- i1++;
- }
- if(s2[j1] == '*'){
- dp[i1][j1] = len + 1;
- j1++;
- }
- if(j + 1 == s2.size()){
- dp[i + 1][j] = len + 1;
- q.push({i + 1, j});
- }
- if(i + 1 == s1.size()){
- dp[i][j + 1] = len + 1;
- q.push({i, j + 1});
- }
- if(i1 < s1.size() && j1 < s2.size() && equals(s1[i1], s2[j1])){
- dp[i1][j1] = len + 1;
- q.push({i1, j1});
- }
- int x = lower_bound(pos1.begin(), pos1.end(), i + 1) - pos1.begin() - 1;
- int y = lower_bound(pos2.begin(), pos2.end(), j + 1) - pos2.begin() - 1;
- if(x >= 0 && dp[pos1[x]][j + 1] >= len + i - x){
- dp[pos1[x]][j + 1] = len + i - x;
- q.push({pos1[x], j + 1});
- }
- if(y >= 0 && dp[i + 1][pos2[y]] >= len + j - y){
- dp[i + 1][pos2[y]] = len + j - y;
- q.push({i + 1, pos2[y]});
- }
- }
- if(dp[s1.size() - 1][s2.size() - 1] != INF){
- cout << s1 << " " << s2 << " " << dp[s1.size() - 1][s2.size() - 1] << endl;
- }
- return (dp[s1.size() - 1][s2.size() - 1] != INF);
- }
- int32_t main(){
- string s1, s2;
- cin >> s1 >> s2;
- cout << check(s1, s2) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement