1、只要把重复出现的删掉再判断就OK了。
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <cstdlib> 5 #include <map> 6 #include <algorithm> 7 #include <stack> 8 #include <queue> 9 #include <cmath> 10 using namespace std; 11 class FauxPalindromes { 12 public: 13 string classifyIt(string word) { 14 int wend = word.size() - 1; 15 int wstart = 0; 16 bool judge = true; 17 while (wstart < wend) { 18 if (word[wstart] != word[wend]) { 19 judge = false; 20 break; 21 } 22 wstart++; 23 wend--; 24 } 25 if(judge) 26 return "PALINDROME"; 27 28 string wordsame; 29 int sz = word.size(); 30 char pre = 'a'; 31 for (int i = 0; i < sz; i++) { 32 if (word[i] == pre) { 33 pre = word[i]; 34 continue; 35 } 36 wordsame += word[i]; 37 pre = word[i]; 38 } 39 wend = wordsame.size() - 1; 40 wstart = 0; 41 judge = true; 42 while (wstart < wend) { 43 if (wordsame[wstart] != wordsame[wend]) { 44 judge = false; 45 break; 46 } 47 wstart++; 48 wend--; 49 } 50 if (judge) 51 return "FAUX"; 52 return "NOT EVEN FAUX"; 53 } 54 55 };
2、分成三次计算,每一次里边的个数都是相同的
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <cstdlib> 5 #include <map> 6 #include <algorithm> 7 #include <stack> 8 #include <queue> 9 #include <cmath> 10 using namespace std; 11 typedef long long ll; 12 ll cn[3][3]; 13 class AlternateColors { 14 public: 15 string choose(ll cn[], ll num) { 16 string c[] = { "RED", "GREEN", "BLUE" }; 17 int co = -1; 18 for (int i = 0; i < 3; i++) { 19 if (cn[i] > 0) { 20 co++; 21 } 22 if (co == num) 23 return c[i]; 24 } 25 return "RED"; 26 } 27 28 string getColor(long long r, long long g, long long b, long long k) { 29 ll cno[] = { r, g, b }; 30 31 for (int i = 0; i < 3; i++) { 32 ll cmin = min(min(cno[0], cno[1]), cno[2]); 33 ll cmax = max(max(cno[0], cno[1]), cno[2]); 34 ll cmid = cno[0] + cno[1] + cno[2] - cmin - cmax; 35 if (cmin == 0) { 36 if (cmid != 0) { 37 cmin = cmid; 38 } else { 39 cmin = cmax; 40 } 41 } 42 for (int j = 0; j < 3; j++) { 43 if (cno[j] >= cmin) { 44 cn[i][j] = cmin; 45 cno[j] -= cmin; 46 } 47 } 48 } 49 50 ll num_color[3] = { 0, 0, 0 }; 51 ll num_total[3] = { 0, 0, 0 }; 52 for (int i = 0; i < 3; i++) { 53 for (int j = 0; j < 3; j++) { 54 if (cn[i][j] > 0) { 55 num_color[i]++; 56 num_total[i] += cn[i][j]; 57 } 58 } 59 } 60 61 for (int i = 0; i < 3; i++) { 62 if (k <= num_total[i]) { 63 ll cur = (k - 1) % num_color[i]; 64 return choose(cn[i], cur); 65 } 66 k = k - num_total[i]; 67 } 68 return "RED"; 69 } 70 71 };
3、在可以移动一次的情况下,把所有的方格看成是重复出现的
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <cstdlib> 5 #include <map> 6 #include <algorithm> 7 #include <stack> 8 #include <queue> 9 #include <cmath> 10 using namespace std; 11 typedef long long ll; 12 int dx[4] = { -1, -1, 1, 1 }; 13 int dy[4] = { -1, 1, -1, 1 }; 14 int n, W, H, A, B; 15 bool vis[1100][1100]; 16 long long dfs(int x, int y) { 17 if (x < 0 || x >= n || x >= W || y < 0 || y >= n || y >= H) 18 return 0; 19 if (vis[x][y]) 20 return 0; 21 vis[x][y] = true; 22 ll res = W / n + (x + 1 <= W % n); 23 res *= H / n + (y + 1 <= H % n); 24 for (int i = 0; i < 4; i++) 25 res = res + dfs(x + dx[i] * A, y + dy[i] * B) 26 + dfs(x + dx[i] * B, y + dy[i] * A); 27 return res; 28 } 29 30 class KnightCircuit { 31 public: 32 long long maxSize(int w, int h, int a, int b) { 33 if ((a >= w && b >= w) || (a >= h && b >= h))//保证可以移动一次 34 return 1; 35 A = a, B = b, n = 2 * a * b, W = w, H = h; 36 ll ans = 0, res; 37 for (int i = 0; i < n && i < W; i++) 38 for (int j = 0; j < n && j < H; j++) 39 if (!vis[i][j]) { 40 res = dfs(i, j); 41 ans = max(res, ans); 42 } 43 return ans; 44 } 45 };
DIV1-2、算法很巧妙,
http://blog.csdn.net/cyberzhg/article/details/8288425
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <cstdlib> 5 #include <map> 6 #include <algorithm> 7 #include <stack> 8 #include <queue> 9 #include <cmath> 10 using namespace std; 11 typedef long long ll; 12 class AlternateColors2 { 13 public: 14 long long countWays(int n, int k) { 15 long long ans = 0; 16 if ((k - 1) % 3 == 0) {//假设后边r、g、b都有 17 int remain = n - k; 18 ans += (long long) (remain + 2) * (remain + 1) / 2; 19 } 20 for (int i = 0; i * 3 < (k - 1); ++i) { 21 int remain = k - 1 - i * 3; 22 ans += remain ;//后边如果只有r 23 if (remain % 2 == 0) {//后边如果有b或者g 24 ans += n - k + 1; 25 ans += n - k + 1; 26 } 27 } 28 return ans; 29 } 30 };