subrat.iitkgp
BAN USERbool isValid(const string& str) {
const string alphabet = "hire";
int curPos = 0;
int count = 0;
int tmpcount = 0;
for (int i = 0; i < str.length(); ++i) {
if (str[i] == 'h') ++count;
else {
// Indicates valid transition from 'h'
if (count && (curPos == 0)) { ++curPos; curPos %= alphabet.length(); tmpcount = 0; }
// Check if current char matches curPOs
if (str[i] != alphabet[curPos]) return false;
++tmpcount;
// Indicates (expected) transition from 'cur' to 'next'
if (tmpcount == count) {
tmpcount = 0;
++curPos; curPos %= alphabet.length();
if (curPos == 0) count = 0; // reset count if we are going to next cycle of 'h'
}
}
}
if (curPos != 0) return false;
if (str[str.length() - 1] != alphabet[alphabet.length() - 1]) return false;
return true;
}
Use Hash maps inside Hash maps.
e.g. in C++
struct StateData {
map<string, int> CityData;
int StatePeopleCount;
}
struct CountryData {
map<string, StateData*> StateData;
int CountryPeopleCount;
}
map<string, CountryData*> CountryDataHash;
Repsandrafdenn, Associate at CareerCup
Je suis développeur web dans une société de Wild Oats Markets, j'adore faire du shopping et j'ai une ...
Repaalexlingram, AT&T Customer service email at ABC TECH SUPPORT
I am Alex and I live in Colorado Springs . I am working as a International human resources manager and I ...
Repgiannanewhart, Cloud Support Associate at ABC TECH SUPPORT
I am Clinical managers, a type of medical and health services manager, and work as managers in both administrative areas ...
Repjenniferdray9, Accountant at ABC TECH SUPPORT
Hi I am Jennifer D. Ray from san Diego.Currently i am working as a parts salesperson in Rite solution ...
Repmartinskrull, Analyst at A9
Hi everyone, I am from new york,USA. I currently work in the Affiliate Marketing industry. I love all things ...
Reppaulaamontalvo, AT&T Customer service email at AMD
I am working as Human Resources Associates, and my duties are for obtaining, recording, and interpreting human resources information within ...
Repdianacloweryd, Developer Program Engineer at Accolite software
I am Diana from Reston USA . I work as an Agricultural and food science technician in Jumbo Sports. I help ...
Reprealspellcaster4, Cloud Support Associate at Aricent
Hi, I am from PA, United states. I believe in making the impossible possible because there’s no fun in ...
Repangilafinch, Applications Developer at Aristocrat Gaming
I am Angila, expert system operator in Linens'n from 2017, Where I interact with network providers and perform troubleshooting ...
Repdelenestanf0, HR Executive at Agilent Technologies
Hi, I am a Clinical social worker. methods of prevention and treatment in providing mental-health/healthcare services, I also have ...
This can be easily done using BFS. The equation I followed is as below:
ProbAlive(after N steps) = ProbAlive(after 1 step)
* ProbAlive(after 2 steps, given alive after 1 step) *
* ...
* ProbAlive(after N steps, given alive after N-1 steps)
To compute the term ProbAlive(after K steps, given alive after K-1 steps), BFS is used.
When the BFS queue has only elements with positions for kth step, sample space is the queue-size. Iterate through all elements in the queue and if position is valid, add a probability of 1/queue-size to ProbAliveCurStep. During that time, enqueue all subsequent positions with step marked as k+1. Before processing queue with positions marked as k+1, update ProbAlive = ProbAlive * ProbAliveCurStep and reset ProbAliveCurStep.
e.g. For a 4x4 Matrix, max_step 2 and initial pos 0, 0
Iteration 1:
Queue Contents: (x: 0, y: 0, step: 0)
Pop Element: (0, 0, 0)
Sample Space = 0; // Initial position is ignored for probability computation
ProbAliveCurStep = 0;
Enqueue Neighbours
Iteration 2:
Queue Contents: (1, 0, 1), (-1, 0, 1), (0, 1, 1), and (0, -1, 1)
Pop Element: (1, 0, 1) // Valid element, proceed
Sample Space = 4
ProbAliveCurStep = 0.25
Enqueue Neighbours
Iteration 3:
Queue Contents: (-1, 0, 1), (0, 1, 1) and (0, -1, 1)
Pop Element: (-1, 0, 1) // Invalid element, continue
...
...
After 13 iterations,
ProbAliveCurStep for Step 1 = 0.5, ProbAliveCurStep for Step 2 = 0.75
ProbAlive = 0.357
- subrat.iitkgp October 31, 2013