您的位置:首页 > 其它

SRM 564 DIV2 DIV1

2012-12-13 16:05 183 查看
1、只要把重复出现的删掉再判断就OK了。

#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
using namespace std;
class FauxPalindromes {
public:
string classifyIt(string word) {
int wend = word.size() - 1;
int wstart = 0;
bool judge = true;
while (wstart < wend) {
if (word[wstart] != word[wend]) {
judge = false;
break;
}
wstart++;
wend--;
}
if(judge)
return "PALINDROME";

string wordsame;
int sz = word.size();
char pre = 'a';
for (int i = 0; i < sz; i++) {
if (word[i] == pre) {
pre = word[i];
continue;
}
wordsame += word[i];
pre = word[i];
}
wend = wordsame.size() - 1;
wstart = 0;
judge = true;
while (wstart < wend) {
if (wordsame[wstart] != wordsame[wend]) {
judge = false;
break;
}
wstart++;
wend--;
}
if (judge)
return "FAUX";
return "NOT EVEN FAUX";
}

};


2、分成三次计算,每一次里边的个数都是相同的

#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
using namespace std;
typedef long long ll;
ll cn[3][3];
class AlternateColors {
public:
string choose(ll cn[], ll num) {
string c[] = { "RED", "GREEN", "BLUE" };
int co = -1;
for (int i = 0; i < 3; i++) {
if (cn[i] > 0) {
co++;
}
if (co == num)
return c[i];
}
return "RED";
}

string getColor(long long r, long long g, long long b, long long k) {
ll cno[] = { r, g, b };

for (int i = 0; i < 3; i++) {
ll cmin = min(min(cno[0], cno[1]), cno[2]);
ll cmax = max(max(cno[0], cno[1]), cno[2]);
ll cmid = cno[0] + cno[1] + cno[2] - cmin - cmax;
if (cmin == 0) {
if (cmid != 0) {
cmin = cmid;
} else {
cmin = cmax;
}
}
for (int j = 0; j < 3; j++) {
if (cno[j] >= cmin) {
cn[i][j] = cmin;
cno[j] -= cmin;
}
}
}

ll num_color[3] = { 0, 0, 0 };
ll num_total[3] = { 0, 0, 0 };
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (cn[i][j] > 0) {
num_color[i]++;
num_total[i] += cn[i][j];
}
}
}

for (int i = 0; i < 3; i++) {
if (k <= num_total[i]) {
ll cur = (k - 1) % num_color[i];
return choose(cn[i], cur);
}
k = k - num_total[i];
}
return "RED";
}

};


3、在可以移动一次的情况下,把所有的方格看成是重复出现的

#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
using namespace std;
typedef long long ll;
int dx[4] = { -1, -1, 1, 1 };
int dy[4] = { -1, 1, -1, 1 };
int n, W, H, A, B;
bool vis[1100][1100];
long long dfs(int x, int y) {
if (x < 0 || x >= n || x >= W || y < 0 || y >= n || y >= H)
return 0;
if (vis[x][y])
return 0;
vis[x][y] = true;
ll res = W / n + (x + 1 <= W % n);
res *= H / n + (y + 1 <= H % n);
for (int i = 0; i < 4; i++)
res = res + dfs(x + dx[i] * A, y + dy[i] * B)
+ dfs(x + dx[i] * B, y + dy[i] * A);
return res;
}

class KnightCircuit {
public:
long long maxSize(int w, int h, int a, int b) {
if ((a >= w && b >= w) || (a >= h && b >= h))//保证可以移动一次
return 1;
A = a, B = b, n = 2 * a * b, W = w, H = h;
ll ans = 0, res;
for (int i = 0; i < n && i < W; i++)
for (int j = 0; j < n && j < H; j++)
if (!vis[i][j]) {
res = dfs(i, j);
ans = max(res, ans);
}
return ans;
}
};


DIV1-2、算法很巧妙,

  http://blog.csdn.net/cyberzhg/article/details/8288425

#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
using namespace std;
typedef long long ll;
class AlternateColors2 {
public:
long long countWays(int n, int k) {
long long ans = 0;
if ((k - 1) % 3 == 0) {//假设后边r、g、b都有
int remain = n - k;
ans += (long long) (remain + 2) * (remain + 1) / 2;
}
for (int i = 0; i * 3 < (k - 1); ++i) {
int remain = k - 1 - i * 3;
ans += remain ;//后边如果只有r
if (remain % 2 == 0) {//后边如果有b或者g
ans += n - k + 1;
ans += n - k + 1;
}
}
return ans;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: