Div 1, Level 3
Div 2, Level 3
Tutorials:
Division One - Level Three:
Solution
Source Code:
Division One - Level Two:
Solution
Breadth First Search (BFS).Problem hints:
- Words can be considered as states. There are at most 26^4 different words composed of 4 letters (thus a linear complexity will run in allowed time).
- There are some ways to pass from one state to another.
- The cost of passing from a state to another is always 1 (i.e. a single button click).
- You need to find the minimum number of steps required to reach the end state from start state.
Source Code:
//Thu May 19 11:12:55 CDT 2011#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
int dx[] = { 0, 0, 1, -1, 0, 0, 0, 0 };
int dy[] = { 0, 0, 0, 0, 1, -1, 0, 0 };
int dz[] = { 0, 0, 0, 0, 0, 0, 1, -1 };
int dw[] = { 1, -1, 0, 0, 0, 0, 0, 0 };
class State {
public:
int a;
int b;
int c;
int d;
State(string s) {
this->a = s[0] - 'a';
this->b = s[1] - 'a';
this->c = s[2] - 'a';
this->d = s[3] - 'a';
}
State(int aa, int bb, int cc, int dd) {
this->a = aa;
this->b = bb;
this->c = cc;
this->d = dd;
}
bool equals(State s) {
return this->a == s.a && this->b == s.b && this->c == s.c && this->d
== s.d;
}
};
class SmartWordToy {
private:
bool forbiden[26][26][26][26];
int cost[26][26][26][26];
public:
int minPresses(string start, string finish, vector<string> forbid) {
init(forbid);
// if (forbiden[start[0] - 'a'][start[1] - 'a'][start[2] - 'a'][start[3]
// - 'a'])
// return -1;
State st(start);
cost[start[0] - 'a'][start[1] - 'a'][start[2] - 'a'][start[3] - 'a']
= 0;
queue<State> q;
q.push(st);
while (!q.empty()) {
int a = q.front().a;
int b = q.front().b;
int c = q.front().c;
int d = q.front().d;
int dist = cost[a][b][c][d];
q.pop();
for (int i = 0; i < 8; i++) {
int x = (a + dx[i] + 26) % 26;
int y = (b + dy[i] + 26) % 26;
int z = (c + dz[i] + 26) % 26;
int w = (d + dw[i] + 26) % 26;
State tmpST(x, y, z, w);
if (forbiden[x][y][z][w] || cost[x][y][z][w] != -1)
continue;
cost[x][y][z][w] = dist + 1;
q.push(tmpST);
}
}
return cost[finish[0] - 'a'][finish[1] - 'a'][finish[2] - 'a'][finish[3]
- 'a'];
}
void init(vector<string> f) {
for (int i = 0; i < 26; i++) {
for (int j = 0; j < 26; j++) {
for (int p = 0; p < 26; p++) {
for (int q = 0; q < 26; q++) {
forbiden[i][j][p][q] = false;
cost[i][j][p][q] = -1;
}
}
}
}
for (int i = 0; i < (int) f.size(); i++) {
vector<string> tmp = split(f[i]);
for (int a = 0; a < (int) tmp[0].size(); a++) {
for (int b = 0; b < (int) tmp[1].size(); b++) {
for (int c = 0; c < (int) tmp[2].size(); c++) {
for (int d = 0; d < (int) tmp[3].size(); d++) {
forbiden[tmp[0][a] - 'a'][tmp[1][b] - 'a'][tmp[2][c]
- 'a'][tmp[3][d] - 'a'] = true;
}
}
}
}
}
}
vector<string> split(string s) {
vector<string> ret;
stringstream ss(s);
while (ss >> s) {
ret.push_back(s);
}
return ret;
}
};
Division One - Level One:
Solution
Same with Div2, Level2.
Source Code:
Division Two - Level Three:
Solution
Source Code:
Division Two - Level Two:
Solution
All cuts / C(N,2).
Source Code:
//SRM233Div2_500;//SRM233DIV1_250;
//2009/10/23 16:28:28
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class PipeCuts
{
public:
double probability(vector <int> weldLocations, int L)
{
int count = 0;
sort(weldLocations.begin(), weldLocations.end());
for(int i=0; i<weldLocations.size(); i++)
{
for(int j=i+1; j<weldLocations.size(); j++)
if((weldLocations[j]-weldLocations[i]>L) || weldLocations[i]>L || (100-weldLocations[j])>L)
count ++;
}
return 2.0 * count / ((weldLocations.size()-1) * weldLocations.size());
}
};
Division Two - Level One:
Solution
Source Code:
//2009/08/15 18:43:18#include <string>
#include <vector>
#include <map>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
class JustifyText
{
public:
vector <string> format(vector <string> text)
{
int sz = 0;
vector<string> ret;
for(int i=0; i<text.size(); i++) sz = max((int)text[i].size(), sz);
for(int i=0; i<text.size(); i++)
{
string s(sz-text[i].size(), ' ');
ret.push_back(s+text[i]);
}
return ret;
}
};
No comments :
Post a Comment