题目链接 :C-游游的问号替换_牛客周赛 Round 15 (nowcoder.com)
可以当成构造题目做, 长度最长只有1000, 并且每次dfs的正确答案只有一两个, 所以时间跑的很快
int main() { string s; cin >> s; int n = s.size(); auto dfs = [&](auto &&dfs, string str, int s) -> string { if(s >= n) return str; char cp = str[s]; for(int i = 0; i <= 2; i ++) { if(cp != '?' && cp - '0' != i) continue;//非?时跳过构造相同情况 str[s] = (char)(i + '0');//构造?等于0.1.2的情况 if(s > 0 && str[s] == str[s - 1]) continue; if(s <= 1) { string res = dfs(dfs, str, s + 1); if(res != "null") return res; } else { int sum = (str[s - 2] - '0' * 9) + (str[s - 1] - ' 0') * 3 + (str[s] - '0'); if(sum & 1) continue; string res = dfs(dfs, str, s + 1); if(res != "null") return res; } } return "null"; }; string res = dfs(dfs, s, 0); cout << (res == "null" ? "-1" : res) << endl; return 0; }
标签:string,int,dfs,游游,str,替换,问号 From: https://www.cnblogs.com/ZouYua/p/17767963.html