Slots of Fun

The International Betting Machine company has just issued a new type of slot machine. The machine display consists of a set of identical circles placed in a triangular shape. An example with four rows is shown below. When the player pulls the lever, the machine places a random letter in the center of each circle. The machine pays offwhenever any set of identical letters form the vertices of an equilateral triangle. In the example below, the letters ‘a’ and ‘c’ satisfy this condition.

In order to prevent too many payoffs, the electronics in the machine ensures that no more than 3 of any letter will appear in any display configuration.

IBM is manufacturing several models of this machine, with varying number of rows in the display, and they are having trouble writing code to identify winning configurations. Your job is to write that code.

Input

Input will consist of multiple problem instances. Each instance will start with an integer n indicating the number of rows in the display. The next line will contain n(n + 1)/2 letters of the alphabet (all lowercase) which are to be stored in the display row-wise, starting from the top. For example, the display above would be specified as

4
abccddadca

The value of n will be between 1 and 12, inclusive. A line with a single 0 will terminate input.

Output

For each problem instance, output all letters which form equilateral triangles on a single line, in alphabetical order. If no such letters exist, output “LOOOOOOOOSER!”.

Sample Input

4
abccddadca
6
azdefccrhijrrmznzocpq
2
abc
0

Sample Output

ac
crz
LOOOOOOOOSER!

思路:

暴力枚舉,設n層等邊三角形最左下角的點爲(0,0),第i層(1開始計數)的第j(0開始計數)個點的座標就是((n-1)/2-(i-1)/2+j,(n-i)*根號3/2)。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <cstdlib>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <list>
using namespace std;
const double G3 = sqrt(3.0);
const double eps = 1e-7;
struct node {
    double x, y;
};
bool judge(node a, node b, node c) {
    double ab, ac, bc;
    ab = sqrt(pow(a.x - b.x, 2.0) + pow(a.y - b.y, 2.0));
    ac = sqrt(pow(a.x - c.x, 2.0) + pow(a.y - c.y, 2.0));
    bc = sqrt(pow(b.x - c.x, 2.0) + pow(b.y - c.y, 2.0));
    if (fabs(ab - ac) <= eps && fabs(ab - bc) <= eps) return true;
    return false;
}
int main() {
    int n;
    while (cin >> n, n) {
        vector <node> pos[26];
        vector <char> ans;
        string text;
        cin >> text;
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j < i; j++) {
                node tmpnode;
                tmpnode.x = (n - 1) * 1.0 / 2 - (i - 1) * 1.0 / 2 + j * 1.0;
                tmpnode.y = G3 * (n - i) * 1.0 / 2;
                pos[text[j] - 'a'].push_back(tmpnode);
                //cout << text[j] << " " << tmpnode.x << " " << tmpnode.y << endl;
            }
            text.erase(0, i);
        }
        for (int i = 0; i < 26; i++) {
            if (pos[i].size() != 3) continue;
            if (judge(pos[i][0], pos[i][1], pos[i][2])) ans.push_back((char)(i + 'a'));
        }
        if (ans.empty()) cout << "LOOOOOOOOSER!" << endl;
        else {
            for (int i = 0; i < ans.size(); i++) cout << ans[i];
            cout << endl;
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章