Hi, can anyone help me figure out why the system test gives a different answer to my computer for this code?. Codeforces Round #425 (Div. 2), Problem B.
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
using namespace std;
bool vali(string test, string s, int alf[300]){
int j = 0, i = 0;
if((int)s.size() == (int)test.size() + 1)
for(; j < (int)s.size(); j++){
if(s[j] == '*'){ continue;}
else if((s[j] == test[i]) || (s[j] == '?' && alf[(int)test[i]] == 1) ){
i++;
continue;
}
else
return false;
}
else if((int)s.size() == (int)test.size()){
for(; j < (int)s.size(); j++){
if(s[j] == '*'){ i++; continue;}
else if((s[j] == test[i]) || (s[j] == '?' && alf[(int)test[i]] == 1) ){
i++;
continue;
}
else
return false;
}
}
if(i == (int)test.size() && j == (int)s.size()) return true;
return false;
}
int main(){
string cad; cin>>cad;
int alf[300];
for(int i = 0; i < (int)cad.size(); i++) alf[i] = 0;
for(int i = 0; i < (int)cad.size(); i++)
alf[(int)cad[i]]++;
string s; cin>>s;
int n; cin>>n;
string test;
bool flag = true;
vector<string> ans;
for(int i = 0; i< n; i++){
cin>>test;
flag = vali(test, s, alf);
if(flag) ans.push_back("YES");
else ans.push_back("NO");
}
for(int i = 0; i< n; i++)
cout<<ans[i]<<endl;
return 0;
}
Thanks!
It is not strange behavior "for(int i = 0; i < (int)cad.size(); i++) alf[i] = 0;" here you only initialized elements to 0 only till size of string . In your local machine if you declare "int alf[300]" they all get initialized to 0 automatically but not on code forces . So you need to change "for(int i = 0; i <=int('z'); i++) alf[i] = 0;". Hope you understood.
I faced the same problem. Used regex to solve the problem and got right answer for pretest 2 on ideone but wrong answer on codeforces. 28850846
One of my friends shubhambhattar helped me figure out the problem. Go here to check a discussion on codechef which explains about online compilers.