domingo, 29 de julio de 2012

10189 - Minesweeper, uva

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#define db(a) cout << #a << " = " << a << endl
#define db2(a, b) cout << #a << " = " << a << " " << #b << " = " << b << endl
using namespace std;
int n, m;
int dx[] = {-1, -1, -1,  0, 0, 1, 1, 1};
int dy[] = {-1,  0,  1, -1, 1, -1, 0, 1};
vector<string> grid;
char tonum(int x, int y) {
 int cont = 0;
 for (int i = 0; i < 8; i++)
  if (x + dx[i] >= 0 && x + dx[i] < n && y + dy[i] >= 0 && y + dy[i] < m)
   if (grid[x + dx[i]][y + dy[i]] == '*') cont++;
 return cont + '0';
}
int main() {
 #ifdef dennisbot
  freopen("in.in", "r", stdin);
  freopen("ou.out", "w", stdout);
 #endif
 string linea;
 int field = 1;
 cin >> n >> m;
 while (1) {
  for (int i = 0; i < n; i++) {
   cin >> linea;
   grid.push_back(linea);
  }
  for (int i = 0; i < n; i++) 
   for (int j = 0; j < m; j++) 
    if (grid[i][j] != '*')
     grid[i][j] = tonum(i, j);
  
  cout << "Field #" << field++ << ":"<< endl;
  for (int i = 0; i < n; i++)
   cout << grid[i] << endl;
  cin >> n >> m;
  if (n == 0 && m == 0) break;
  cout << endl;
  grid.clear();
 }
 return 0;
}

martes, 17 de julio de 2012

11734 - Big Number of Teams will Solve This, uva

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#define db(a) cout << #a << " = " << a << endl
#define db2(a, b) cout << #a << " = " << a << " " << #b << " = " << b << endl
using namespace std;
int main() {
 #ifdef dennisbot
  freopen("in.in", "r", stdin);
  freopen("ou.out", "w", stdout);
 #endif
 int t;
 scanf("%d\n", &t) ;
 char *team, *judge;
 char line[30], line2[30];
 bool wa , pe;
 for (int i = 1; i <= t; i++) {
  gets(line);gets(line2);
  team = line; judge = line2;
  wa = pe = false;
  for ( ; *team && !wa; team++) {
   if (*team == *judge) {
    judge++;
    continue;
   }
   if(*team == ' ') {
    pe = true;
    continue;
   }
   if(*team != *judge) wa = true;
  }
  if (*team != *judge) wa = true;
  
  if (!pe && !wa)
    printf("Case %d: Yes\n", i); 
  else if (wa)
    printf("Case %d: Wrong Answer\n", i); 
   else
    printf("Case %d: Output Format Error\n", i);     
 }
 return 0;
}

148 - Anagram checker, uva

#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<set>
#include<map>
#define db(a) \
cout << #a << " = " << a << endl
#define db2(a, b) \
cout << #a << " = " << a << " " << #b << " = " << b << endl
#define inf (1<<30)
#define foreach(m, it) \
for (typeof(m.begin()) it = m.begin(); it != m.end(); it++)
using namespace std;
struct word {
 string str; char trait[26];
 bool operator >= (const word& other) {
  int i = 0;
  for (; i < 26 && trait[i] >= other.trait[i]; i++);
  return i == 26;
 }
 word& operator -= (const word& other) {
  for (int i = -1; ++i < 26; trait[i] -= other.trait[i]);
  return *this;
 }
 word& operator += (const word& other) {
  for (int i = -1; ++i < 26; trait[i] += other.trait[i]);
  return *this;
 }
};
void genTrait(const char *str, char * trait) {
 for (fill(&trait[0], &trait[26], 0); *str != 0; ++trait[*str++ - 'A']);
}
typedef vector<word>::iterator iterpal;
void searchAnagram(iterpal ini, iterpal fin, word& frase) {
 int i = 0;
 string &str = frase.str;
 for (; i < 26 && frase.trait[i] == 0; i++);
 if (i == 26) {
  istringstream ss(str);
  vector<string> original, generado;
  for (string word; ss >> word && word != "="; original.push_back(word));
  for (string word; ss >> word ; generado.push_back(word));
  sort(original.begin(), original.end());
  if (original != generado) {
   printf("%s\n", str.c_str());
  }
  return;
 }
 for (iterpal it = ini; it != fin; it++) {
  if (frase >= *it) {
   frase -= *it;
   str.push_back(' ');
   str.append(it->str);
   searchAnagram(it + 1, fin, frase);
   str.erase(str.length() - it->str.length() - 1);
   frase += *it;
  }
 }
 
}
int main() {
 #ifdef dennisbot
  freopen("in.in", "r", stdin);
  freopen("ou.out", "w", stdout);
 #endif
 vector<word> dict, subset;
 char line[30];
 for (word palabra; gets(line) != NULL && line[0] != '#'; ) {
  palabra.str = line;
  genTrait(line, palabra.trait);
  dict.push_back(palabra);
 }
 for (string linea; getline(cin, linea) && linea != "#"; subset.clear()) {
  word frase = {linea};
  linea.erase(remove(linea.begin(), linea.end(), ' '), linea.end());
  if (linea.empty()) continue;
  
  genTrait(linea.c_str(), frase.trait);
  for (iterpal it = dict.begin(); it != dict.end(); it++) {
   if (frase >= *it) {
    subset.push_back(*it);
   }
  }
  frase.str.append(" =");
  searchAnagram(subset.begin(), subset.end(), frase);
 }
    return 0;
}

viernes, 13 de julio de 2012

263 - Number Chains, uva

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<set>
#include<map>
#define db(a) \
cout << #a << " = " << a << endl
#define db2(a, b) \
cout << #a << " = " << a << " " << #b << " = " << b << endl
#define inf (1<<30)
#define foreach(m, it) \
for (typeof(m.begin()) it = m.begin(); it != m.end(); it++)
using namespace std;
int n;
bool espali(string linea) {
 int n = linea.size();
 for (int i = 0; i < n / 2; i++) {
  if (linea[i] != linea[n - i - 1])
   return false;
 }
 return true;
}
int main() {
 #ifdef dennisbot
  freopen("in.in", "r", stdin);
  freopen("ou.out", "w", stdout);
 #endif
 string s;
 char buf[10];
 int mayor, menor, prev, len;
 set<int> nums;
 while (cin >> s) {
  if (s == "0") break;
  prev = -1;
  len = 0;
  nums.clear();
  printf("Original number was %s\n", s.c_str());
  while (true) {
   sort(s.rbegin(), s.rend());
   mayor = atoi(s.c_str());
   reverse(s.begin(), s.end());
   menor = atoi(s.c_str());
   printf("%d - %d = %d\n", mayor, menor, mayor - menor);
   len++;
   prev = mayor - menor;
   if (!nums.count(prev))
    nums.insert(prev);
   else break;
   sprintf(buf, "%d", prev);
   s = buf;
  }
  printf("Chain length %d\n\n", len);
 }
 return 0;
}

Here is the Statement

353 - Pesky Palindromes, uva

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<set>
#include<map>
#define db(a) \
cout << #a << " = " << a << endl
#define db2(a, b) \
cout << #a << " = " << a << " " << #b << " = " << b << endl
#define inf (1<<30)
#define foreach(m, it) \
for (typeof(m.begin()) it = m.begin(); it != m.end(); it++)
using namespace std;
int n;
bool espali(string linea) {
 int n = linea.size();
 for (int i = 0; i < n / 2; i++) {
  if (linea[i] != linea[n - i - 1])
   return false;
 }
 return true;
}
int main() {
 #ifdef dennisbot
  freopen("in.in", "r", stdin);
  freopen("ou.out", "w", stdout);
 #endif
 char linea[100];
 set<string> s;
 while (gets(linea) != NULL) {
  int n = strlen(linea);
  s.clear();
  for (int i = 0; i < n; i++) {
   for (int j = i; j < n; j++) {
    string aux = string(linea).substr(i, j - i + 1);
    if (espali(aux))
     s.insert(aux);
   }
  }
  printf("The string '%s' contains %d palindromes.\n", linea , s.size());
 }
 return 0;
}

Here is the Statement

409 - Excuses, Excuses, uva

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<set>
#include<map>
#define db(a) \
cout << #a << " = " << a << endl
#define db2(a, b) \
cout << #a << " = " << a << " " << #b << " = " << b << endl
#define inf (1<<30)
#define foreach(m, it) \
for (typeof(m.begin()) it = m.begin(); it != m.end(); it++)
using namespace std;
string limpiar(string s) {
 int n = s.size();
 for (int i = 0; i < n; i++) {
  if (!isalpha(s[i])) s[i] = ' ';
  else s[i] = tolower(s[i]);
 }
 return s;
}
bool contain(vector<string> & lista, string keyword) {
 int n = lista.size();
 for (int i = 0; i < n; i++) {
  if (lista[i] == keyword)
   return true;
 }
 return false;
}
int main() {
 #ifdef dennisbot
  freopen("in.in", "r", stdin);
  freopen("ou.out", "w", stdout);
 #endif
 int k, x;
 vector<string> keywords;
 map<string, int> excuses;
 char keyword[40], excuse[100];
 int cont = 1;
 while (scanf("%d %d\n", &k, &x) != EOF) {
  keywords.clear();
  excuses.clear();
  for (int i = 0; i < k; i++) {
   gets(keyword);
   keywords.push_back(keyword);
  }
  for (int i = 0; i < x; i++) {
   gets(excuse);
   excuses[(string)excuse];
  }
  int best = 0;
  foreach(excuses, it) {
   string cad = limpiar(it->first);
   char *st, *buf, sep[] = " ";
   buf = strdup(cad.c_str());
   st = strtok(buf, sep);
   while (st) {
    if (contain(keywords, st))
     it->second++;
    st = strtok(0, sep);
   }
   best = max(it->second, best);
  }
  printf("Excuse Set #%d\n", cont++);
  foreach(excuses, it) {
   if (it->second == best)
    printf("%s\n", it->first.c_str());
  }
  puts("");
 }
 return 0;
}
Here is the Statement

401 - Palindromes, uva

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
#define db(a) \
cout << #a << " = " << a << endl
#define db2(a, b) \
cout << #a << " = " << a << " " << #b << " = " << b << endl
#define inf (1<<30)
#define foreach(it, m) \
for (typeof(m.begin()) it = m.begin(); it != m.end(); it++)
using namespace std;
map<char, char> m;
bool espali(string s) {
 int n = s.size();
 for (int i = 0; i < n / 2; i++) {
  if (s[i] != s[n - i - 1])
   return false;
 }
 return true;
}
bool esmirror(string s) {
 int n = s.size();
 for (int i = 0; i <= n / 2; i++)
  if (s[i] != m[s[n - i - 1]])
   return false;
 return true;
}
int main() {
 #ifdef dennisbot
  freopen("in.in", "r", stdin);
  freopen("ou.out", "w", stdout);
 #endif
 char s[30];
 m['A'] = 'A'; m['M'] = 'M'; m['Y'] = 'Y';
 m['B'] = '-'; m['N'] = '-'; m['Z'] = '5';
 m['C'] = '-'; m['O'] = 'O'; m['1'] = '1';
 m['D'] = '-'; m['P'] = '-'; m['2'] = 'S';
 m['E'] = '3'; m['Q'] = '-'; m['3'] = 'E';
 m['F'] = '-'; m['R'] = '-'; m['4'] = '-';
 m['G'] = '-'; m['S'] = '2'; m['5'] = 'Z';
 m['H'] = 'H'; m['T'] = 'T'; m['6'] = '-';
 m['I'] = 'I'; m['U'] = 'U'; m['7'] = '-';
 m['J'] = 'L'; m['V'] = 'V'; m['8'] = '8';
 m['K'] = '-'; m['W'] = 'W'; m['9'] = '-';
 m['L'] = 'J'; m['X'] = 'X'; 
 bool pali, mirror;
 while(gets(s) != NULL) {
  pali = espali(s);
  mirror = esmirror(s);
  if (pali && mirror)
   printf("%s -- is a mirrored palindrome.\n", s);
  else 
   if (pali) 
    printf("%s -- is a regular palindrome.\n", s);
   else
    if (mirror)
     printf("%s -- is a mirrored string.\n", s);
    else
     printf("%s -- is not a palindrome.\n", s);
  puts("");
 }
 return 0;
}

here is the statement

jueves, 12 de julio de 2012

10878 - Decode the tape, uva

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
#define db(a) \
cout << #a << " = " << a << endl
#define db2(a, b) \
cout << #a << " = " << a << " " << #b << " = " << b << endl
#define inf (1<<30)
#define foreach(it, m) \
for (typeof(m.begin()) it = m.begin(); it != m.end(); it++)
using namespace std;
int main() {
 #ifdef dennisbot
  freopen("in.in", "r", stdin);
  freopen("ou.out", "w", stdout);
 #endif
 char s[20];
 while (gets(s) != NULL) {
  if (s[0] == '_') continue;
  int mask = 0;
  for (int i = 8; i >= 4; i--) {
   if (s[9 - i] == 'o') mask |= 1 << (i - 1);
  }
  for (int i = 2; i >= 0; i--) {
   if (s[9 - i] == 'o') mask |= 1 << i;
  }
  printf("%c", (char)mask);
 }
 return 0;
}
here is the statement

471 - Magic Numbers, uva

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
#define db(a) \
cout << #a << " = " << a << endl
#define db2(a, b) \
cout << #a << " = " << a << " " << #b << " = " << b << endl
#define inf (1<<30)
#define foreach(it, m) \
for (typeof(m.begin()) it = m.begin(); it != m.end(); it++)
using namespace std;
bool has_repeated_digits(long long int s) {
 int mask = 0;
 while (s) {
  if (mask & (1 << (s % 10)))
   return true;
  mask |= 1 << (s % 10);
  s /= 10;
 }
 return false;
}

int main() {
 #ifdef dennisbot
  freopen("in.in", "r", stdin);
  freopen("ou.out", "w", stdout);
 #endif
 int t;
 long long int max_s1 = 9876543210, s1, s2, max_s2, N;
 scanf("%d", &t);
 for (; t--;) {
  scanf("%lld", &N);
  max_s2 = max_s1 / N;
  for (s2 = 1; s2 <= max_s2; s2++) {
   if (has_repeated_digits(s2)) continue;
   s1 = s2 * N;
   if (has_repeated_digits(s1)) continue;
   printf("%lld / %lld = %lld\n", s1, s2, N);
  }
  if (t)
   puts("");
 }
 return 0;
}

here is the statement