Hướng dẫn giải của Quân Joker Rô
Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này
Code mẫu của flashmt
const fi=''; fo=''; maxn=505; var n,m,re,x,y:longint; a:array[0..maxn,0..maxn] of char; vv,v,p,q,r,s:array[0..maxn,0..maxn] of longint; function min(a,b:longint):longint; begin if a<=b then min:=a else min:=b; end; procedure pr; var i,j,t:longint; c:char; begin assign(input,fi); reset(input); readln(m,n); for i:=1 to m do begin for j:=1 to n do begin read(a[i,j]); if a[i,j]=a[i-1,j] then v[i,j]:=v[i-1,j]+1 else v[i,j]:=0; if a[i,j]=a[i,j-1] then p[i,j]:=min(p[i,j-1]+1,v[i,j]) else p[i,j]:=0; end; readln; for j:=n downto 1 do if a[i,j]=a[i,j+1] then r[i,j]:=min(r[i,j+1]+1,v[i,j]) else r[i,j]:=0; end; x:=0; y:=0; re:=-1; for i:=m downto 1 do begin for j:=n downto 1 do begin if a[i,j]=a[i+1,j] then vv[i,j]:=vv[i+1,j]+1 else vv[i,j]:=0; if a[i,j]=a[i,j+1] then q[i,j]:=min(q[i,j+1]+1,vv[i,j]) else q[i,j]:=0; end; for j:=1 to n do begin if a[i,j]=a[i,j-1] then s[i,j]:=min(s[i,j-1]+1,vv[i,j]) else s[i,j]:=0; t:=min(min(p[i,j],q[i,j]),min(r[i,j],s[i,j])); if t>=re then begin if (t>re) or (i<x) or ((i=x) and (j<y)) then begin re:=t; x:=i; y:=j; end; end; end; end; close(input); end; procedure wf; begin assign(output,fo); rewrite(output); write(x,' ',y,' ',re); close(output); end; begin pr; wf; end.
Code mẫu của ladpro98
program DIAMOND; uses math; const maxn=505; fi=''; var a:array[0..maxn,0..maxn] of char; f1,f2,f3,f4:array[0..maxn,0..maxn] of longint; res,n,m,x,y:longint; procedure input; var inp:text; i,j:longint; begin assign(inp,fi);reset(inp); readln(inp,m,n); for i:=1 to m do begin for j:=1 to n do read(inp,a[i,j]); readln(inp); end; close(inp); end; procedure dp; var i,j,temp:longint; begin for i:=1 to M do for j:=1 to n do begin if (a[i-1,j]<>a[i,j]) or (a[i,j-1]<>a[i,j]) then f1[i,j]:=1 else f1[i,j]:=min(f1[i-1,j],f1[i,j-1])+1; end; for i:=1 to M do for j:=n downto 1 do begin if (a[i-1,j]<>a[i,j]) or (a[i,j+1]<>a[i,j]) then f2[i,j]:=1 else f2[i,j]:=min(f2[i-1,j],f2[i,j+1])+1; end; for i:=M downto 1 do for j:=1 to n do begin if (a[i+1,j]<>a[i,j]) or (a[i,j-1]<>a[i,j]) then f3[i,j]:=1 else f3[i,j]:=min(f3[i+1,j],f3[i,j-1])+1; end; for i:=M downto 1 do for j:=n downto 1 do begin if (a[i+1,j]<>a[i,j]) or (a[i,j+1]<>a[i,j]) then f4[i,j]:=1 else f4[i,j]:=min(f4[i+1,j],f4[i,j+1])+1; end; for i:=1 to M do for j:=1 to n do begin temp:=min( min(f1[i,j],f2[i,j]), min(f3[i,j],f4[i,j])); if res<temp then begin res:=temp; x:=i; y:=j; end; end; end; begin input; dp; write(x,' ',y,' ',res-1); end.
Code mẫu của RR
uses math; const FINP = ''; FOUT = ''; MAXN = 555; var f1,f2 : text; m,n : longint; a : array[-1..MAXN,-1..MAXN] of char; d1,d2,d3,d4 : array[-1..MAXN,-1..MAXN] of longint; procedure openF; begin assign(f1,FINP); reset(f1); assign(f2,FOUT); rewrite(f2); end; procedure closeF; begin close(f1); close(f2); end; procedure inp; var i,j:longint; begin readln(f1,m,n); for i:=1 to m do begin for j:=1 to n do read(f1,a[i,j]); readln(f1); end; end; procedure solve; var now,i,j,res,li,lj:longint; begin for i:=1 to m do begin for j:=1 to n do if (a[i,j]=a[i-1,j]) and (a[i,j]=a[i,j-1]) then d1[i,j]:=min(d1[i-1,j],d1[i,j-1])+1 else d1[i,j]:=1; for j:=n downto 1 do if (a[i,j]=a[i-1,j]) and (a[i,j]=a[i,j+1]) then d2[i,j]:=min(d2[i-1,j],d2[i,j+1])+1 else d2[i,j]:=1; end; for i:=m downto 1 do begin for j:=1 to n do if (a[i,j]=a[i+1,j]) and (a[i,j]=a[i,j-1]) then d3[i,j]:=min(d3[i+1,j],d3[i,j-1])+1 else d3[i,j]:=1; for j:=n downto 1 do if (a[i,j]=a[i+1,j]) and (a[i,j]=a[i,j+1]) then d4[i,j]:=min(d4[i+1,j],d4[i,j+1])+1 else d4[i,j]:=1; end; res:=0; for i:=1 to m do for j:=1 to n do begin now:=min(min(d1[i,j],d2[i,j]),min(d3[i,j],d4[i,j])); if now>res then begin res:=now; li:=i; lj:=j; end; end; writeln(f2,li,' ',lj,' ',res-1); end; begin openF; inp; solve; closeF; end.
Code mẫu của hieult
#include <set> #include <map> #include <list> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <stack> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <cassert> #include <cstdlib> #include <cstring> #include <sstream> #include <iomanip> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; //typedef long double ld; typedef double ld; typedef unsigned int ui; typedef unsigned long long ull; #define Rep(i,n) for(__typeof(n) i = 0; i < (n); ++i) #define Repd(i,n) for(__typeof(n) i = (n)-1; i >= 0; --i) #define For(i,a,b) for(__typeof(b) i = (a); i <= (b); ++i) #define Ford(i,a,b) for(__typeof(a) i = (a); i >= (b); --i) #define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i) #define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i) #define mp make_pair #define pb push_back #define fi first #define se second #define sz(a) ((int)(a).size()) #define all(a) (a).begin(), (a).end() #define ms(a,x) memset(a, x, sizeof(a)) #define nl puts("") #define sp printf(" ") #define ok puts("ok") //#include <conio.h> template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; } template<class T> void db(T a, int p = -1) { if (p >= 0) cout << fixed << setprecision(p); cout << a << " "; } template<class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; } template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template<class T> T sqr(T x) { return x * x; } template<class T> T cube(T x) { return x * x * x; } template<class T> struct Triple { T x, y, z; Triple() {} Triple(T _x, T _y, T _z) : x(_x), y(_y), z(_z) {} }; template<class T> Triple<T> euclid(T a, T b) { if (b == 0) return Triple<T>(1, 0, a); Triple<T> r = euclid(b, a % b); return Triple<T>(r.y, r.x - a / b * r.y, r.z); } template<class T> int getbit(T s, int i) { return (s >> i) & 1; } template<class T> T onbit(T s, int i) { return s | (T(1) << i); } template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); } template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); } const int bfsz = 1 << 16; char bf[bfsz + 5]; int rsz = 0;int ptr = 0; char gc() { if (rsz <= 0) { ptr = 0; rsz = fread(bf, 1, bfsz, stdin); if (rsz <= 0) return EOF; } --rsz; return bf[ptr++]; } void ga(char &c) { c = EOF; while (!isalpha(c)) c = gc(); } int gs(char s[]) { int l = 0; char c = gc(); while (isspace(c)) c = gc(); while (c != EOF && !isspace(c)) { s[l++] = c; c = gc(); } s[l] = '\0'; return l; } template<class T> bool gi(T &v) { v = 0; char c = gc(); while (c != EOF && c != '-' && !isdigit(c)) c = gc(); if (c == EOF) return false; bool neg = c == '-'; if (neg) c = gc(); while (isdigit(c)) { v = v * 10 + c - '0'; c = gc(); } if (neg) v = -v; return true; } const double PI = 2 * acos(0); const string months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int dr[] = {0, 0, -1, +1}; const int dc[] = {-1, +1, 0, 0}; const int inf = (int)1e9 + 5; const ll linf = (ll)1e16 + 5; const double eps = ld(1e-9); const ll mod = 100000007; typedef pair<int, int> II; #define maxn 505 #define maxv 1005 int x[maxn][maxn], u[maxn][maxn]; char s[maxn]; int n, m; vector<vector<int> > cal(int a[][maxn]){ vector<vector<int> > res; res.resize(n); Rep(i, n) res[i].resize(m); int t; Rep(i, n) Rep(j, m){ if(i == 0 || j == 0){ res[i][j] = 1; continue;} if(a[i][j] != a[i - 1][j] || a[i][j] != a[i][j - 1]) { res[i][j] = 1; continue;} if(res[i][j - 1] <= res[i - 1][j]){ t = res[i][j - 1]; res[i][j] = t - 1; if(a[i][j + t - 1] == a[i][j]){ res[i][j] = t; if(a[i][j + t] == a[i][j]) res[i][j] = t + 1; } } else{ t = res[i - 1][j]; res[i][j] = t; if(a[i][j + t] == a[i][j]) res[i][j] = t + 1; } } return res; } int main(){ // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); gi(n); gi(m); ms(x, -1); ms(u, -1); Rep(i, n){ gs(s); Rep(j, m){ x[i][j] = s[j] - 'A'; u[n - i - 1][m - j - 1] = x[i][j]; } } vector<vector<int> > V1 = cal(x), V2 = cal(u); int res = 0, temp; II kq; Rep(i, n) Rep(j, m) { temp = min(V1[i][j], V2[n - 1 - i][m - 1 - j]); if(res < temp){ res = temp; kq = mp(i, j); } } cout << kq.fi + 1<< " " << kq.se + 1<< " " << res - 1; return 0; }
Code mẫu của ll931110
#include <algorithm> #include <bitset> #include <cmath> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <vector> using namespace std; char a[505][505]; int m,n; int UL[505][505],UR[505][505],BL[505][505],BR[505][505]; int main() { // freopen("joker.in","r",stdin); // freopen("joker.ou","w",stdout); scanf("%d %d", &m, &n); for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) while (1) { scanf("%c", &a[i][j]); if ('A' <= a[i][j] && a[i][j] <= 'Z') break; } for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) UL[i][j] = (a[i][j] != a[i - 1][j] || a[i][j] != a[i][j - 1]) ? 0 : (1 + min(UL[i - 1][j],UL[i][j - 1])); for (int i = 1; i <= m; i++) for (int j = n; j; j--) UR[i][j] = (a[i][j] != a[i - 1][j] || a[i][j] != a[i][j + 1]) ? 0 : (1 + min(UR[i - 1][j],UR[i][j + 1])); for (int i = m; i; i--) for (int j = 1; j <= n; j++) BL[i][j] = (a[i][j] != a[i + 1][j] || a[i][j] != a[i][j - 1]) ? 0 : (1 + min(BL[i + 1][j],BL[i][j - 1])); for (int i = m; i; i--) for (int j = n; j; j--) BR[i][j] = (a[i][j] != a[i + 1][j] || a[i][j] != a[i][j + 1]) ? 0 : (1 + min(BR[i + 1][j],BR[i][j + 1])); int crx = 0,cry = 0,ret = -1; for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) { int ans = min(UL[i][j],UR[i][j]); ans = min(ans,BL[i][j]); ans = min(ans,BR[i][j]); if (ret < ans) { ret = ans; crx = i; cry = j; } } printf("%d %d %d\n", crx, cry, ret); }
Bình luận