Created
June 19, 2014 23:39
-
-
Save KPCCoiL/3facf39dbb87c705f9be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// {{{ $VIMCODER$ <-------- | |
// vim:filetype=cpp:foldmethod=marker:foldmarker={{{,}}} | |
#include <algorithm> | |
#include <vector> | |
#include <iostream> | |
#include <sstream> | |
#include <cmath> | |
#include <queue> | |
#include <unordered_map> | |
#include <numeric> | |
#include <set> | |
#include <cstring> | |
using namespace std; | |
// }}} | |
class ConundrumReloaded { | |
public: | |
string anss; | |
int n; | |
string btoc = "LH"; | |
int dp[50][2][2]; | |
int minimumLiars(string answers) { | |
anss = answers; | |
memset(dp,-1,sizeof dp); | |
n = answers.size(); | |
int res = min(dfs(0,0,0),dfs(0,1,1)); | |
return res == 100?-1:res; | |
} | |
int dfs(int pos,bool isLiar,bool fstIsLiar) { | |
if(dp[pos][isLiar][fstIsLiar] != -1) return dp[pos][isLiar][fstIsLiar]; | |
if(pos == n-1) { | |
bool last = isLiar ^ fstIsLiar; | |
return anss[pos] != btoc[last]?isLiar:100; | |
} | |
int res = 100; | |
if(anss[pos] == 'H') res = min(res,dfs(pos+1,isLiar,fstIsLiar)+isLiar); | |
else if(anss[pos] == 'L') res = min(res,dfs(pos+1,!isLiar,fstIsLiar)+isLiar); | |
else res = min({res,dfs(pos+1,isLiar,fstIsLiar)+isLiar,dfs(pos+1,!isLiar,fstIsLiar)+isLiar}); | |
return dp[pos][isLiar][fstIsLiar] = res; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment