Skip to content

Instantly share code, notes, and snippets.

@1119-2916
Created February 13, 2017 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1119-2916/ef8bb49bfec2fc98e11265e2b19b840e to your computer and use it in GitHub Desktop.
Save 1119-2916/ef8bb49bfec2fc98e11265e2b19b840e to your computer and use it in GitHub Desktop.
AOJ1316
#include <bits/stdc++.h>
using namespace std;
#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define mp make_pair
#define i_i pair<int, int>
#define vi vector<int>
#define vvi vector<vi >
#define vb vector<bool>
#define vvb vector<vb >
#define vp vector< i_i >
#define all(a) (a).begin(), (a).end()
#define Int(x) int x; scanf("%lld", &x);
//int dxy[5] = {0, 1, 0, -1, 0};
int dx[8] = {1, 1, 1, 0, 0, -1, -1, -1};
int dy[8] = {1, 0, -1, 1, -1, 1, 0, -1};
// assign
string comp(string a, string b);
signed main()
{
int n, m;
while (cin >> n >> m, n) {
vector<string> donut(n);
rep(i, n) {
cin >> donut[i];
}
set<string> can;
string ans = "z";
rep(i, n) {
rep(j, m) {
rep(k, 8) {
vvb used(n, vb(m, false));
string spell;
int x = i, y = j;
while (!used[x][y]) {
spell.pb(donut[x][y]);
used[x][y] = true;
if (can.find(spell) != can.end()) {
ans = comp(ans, spell);
} else {
can.insert(spell);
}
x = (x + dx[k] + n) % n; y = (y + dy[k] + m) % m;
}
}
}
}
cout << ((ans.length() > 1) ? ans : "0") << endl;
}
return 0;
}
string comp(string a, string b)
{
if (a.length() < b.length()) {
return b;
} else if (a.length() > b.length()) {
return a;
} else {
rep(i, a.length()) {
if (a[i] < b[i]) {
return a;
} else if (a[i] > b[i]) {
return b;
}
}
}
return a;
}
@1119-2916
Copy link
Author

How to use "set"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment