Hướng dẫn giải của Dãy con chung dài nhất (new ver)


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.

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=1001;
var n,re,m:longint;
    a:array[1..10,1..maxn] of longint;
    d:array[1..10,1..maxn] of longint;
    f:array[1..maxn] of longint;

procedure rf;
var i,j:longint;
begin
     assign(input,fi); reset(input);
     readln(n,m);
     for i:=1 to m do
         for j:=1 to n do
         begin
              read(a[i,j]);
              d[i,a[i,j]]:=j;
         end;
     close(input);
end;

function check(x,y:longint):boolean;
var i:longint;
begin
     check:=false;
     for i:=1 to m do
         if d[i,x]<d[i,y] then exit;
     check:=true;
end;

procedure pr;
var i,j:longint;
begin
     for i:=1 to n do f[i]:=1;
     for i:=2 to n do
         for j:=1 to i-1 do
             if check(a[1,i],a[1,j]) and (f[j]>=f[i]) then
                f[i]:=f[j]+1;
     re:=0;
     for i:=1 to n do
         if f[i]>re then re:=f[i];
end;

procedure wf;
begin
     assign(output,fo); rewrite(output);
     writeln(re);
     close(output);
end;

begin
     rf;
     pr;
     wf;
end.

Code mẫu của happyboy99x

#include<cstdio>
#include<algorithm>
using namespace std;

#define N 1000
#define M 10

int pos[M][N], n, m, f[N], a[M][N];

bool ok(int x, int y) {
    for(int i = 0; i < m; ++i)
        if(pos[i][x] > pos[i][y]) return 0;
    return 1;
}

int main() {
    scanf("%d%d",&n,&m);
    for(int i = 0; i < m; ++i) 
        for(int j = 0; j < n; ++j) {
            scanf("%d", &a[i][j]);
            pos[i][--a[i][j]] = j;
        }
    for(int i = 0; i < n; ++i) {
        f[i] = 1;
        for(int j = 0; j < i; ++j) 
            if(ok(a[0][j], a[0][i])) f[i] = max(f[i], f[j]+1);
    }
    int res = 1;
    for(int i = 0; i < n; ++i) res = max(res, f[i]);
    printf("%d\n", res);
    return 0;
}

Code mẫu của ladpro98

program lqdgonme;
uses    math;
const   maxn=1010;
        maxm=11;
        fi='';
type    e=record
        x:longint;
        p:array[1..maxm] of longint;
        end;
var     a:array[1..maxn] of e;
        f:array[1..maxn] of longint;
        m,n:longint;

procedure input;
var     inp:text;
        i,j,c:longint;
begin
        assign(inp,fi);
        reset(inp);
        readln(inp,n,m);
        for j:=1 to n do
        begin
                read(inp,c);
                a[c].x:=j;
        end;
        for i:=2 to m do
        begin
                for j:=1 to n do
                begin
                        read(inp,c);
                        a[c].p[i]:=j;
                end;
        end;
        close(inp);
end;

procedure swap(i,j:longint);
var     t:e;
begin
        t:=a[i];
        a[i]:=a[j];
        a[j]:=t;
end;

procedure sort(l,r:longint);
var     i,j,p:longint;
begin
        if l>=r then exit;
        i:=l;j:=r;
        p:=a[random(r-l+1)+l].x;
        repeat
                while a[i].x<p do inc(i);
                while a[j].x>p do dec(j);
                if i<=j then
                begin
                        if i<j then swap(i,j);
                        inc(i);dec(j);
                end;
        until i>j;
        sort(l,j);sort(i,r);
end;

function smaller(i,j:longint):boolean;
var     k:longint;
begin
        for k:=2 to m do
        if a[i].p[k]>a[j].p[k] then exit(false);
        exit(true);
end;

procedure dp;
var     i,j:longint;
begin
        f[1]:=1;
        for i:=2 to n do
        begin
                f[i]:=1;
                for j:=i-1 downto 1 do
                if smaller(j,i) then
                f[i]:=max(f[i],f[j]+1);
        end;
end;

procedure output;
var     oup:text;
        i,res:longint;
begin
        res:=0;
        for i:=1 to n do
        res:=max(res,f[i]);
        write(res);
end;

begin
        input;
        sort(1,n);
        dp;
        output;
end.

Code mẫu của RR

{$MODE OBJFPC}

uses math;
const
  FINP          =       '';
  FOUT          =       '';
  MAXN          =       1011;

var
  f1,f2         :       text;
  n,m           :       longint;
  a,pos         :       array[1..10,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
      read(f1,n,m);
      for i:=1 to m do
        for j:=1 to n do
          begin
            read(f1,a[i,j]);
            pos[i,a[i,j]]:=j;
          end;
    end;

var
  kq            :       array[1..MAXN] of longint;

function check(u,v:longint):boolean;
    var
      i:longint;
    begin
      for i:=2 to m do
        if pos[i,u]>pos[i,v] then exit(false);
      exit(true);
    end;

procedure solve;
    var
      i,j,res:longint;
    begin
      res:=0;
      for i:=1 to n do
        begin
          kq[i]:=1;
          for j:=1 to i-1 do
            if check(a[1,j],a[1,i]) then
              kq[i]:=max(kq[i],kq[j]+1);
          res:=max(res,kq[i]);
        end;
      writeln(f2,res);
    end;

begin
  openF;
  inp;
  solve;
  closeF;
end.

Code mẫu của hieult

#include<bits/stdc++.h>
#define FOR(i,a,b) for(i=a;i<=b;i++)
#define FOR2(i,a,b) for(i=a;i<b;i++)
#define IFOR(i,a,b) for(i=a;i>=b;i--)
#define IFOR2(i,a,b) for(i=a;i>b;i--)
#define IND(a) scanf("%d",&a)
#define IND2(a,b) scanf("%d%d",&a,&b)
#define IND3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define IND4(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)
#define INL(a) scanf("%I64d",&a)
#define INL2(a,b) scanf("%I64d%I64d",&a,&b)
#define INL3(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)
#define INS(s) scanf("%s",&s)
#define OUTD(a) printf("%d ",a)
#define OUTD2(a,b) printf("%d %d ",a,b)
#define OUTD3(a,b,c) printf("%d %d %d",a,b,c)
#define OUTL(a) printf("%I64d ",a)
#define OUTF(a) printf("%.12lf ",a);
#define pb push_back
#define mid ((l+r)>>1)
#define PI acos(-1)
#define ll long long
using namespace std;
const int MOD=1000000007;
const double eps=1e-8;
const int nm=1005;
int n,k,m,t;
ll res;
int a[12][nm],vt[12][nm];
char s[nm];
bool check[nm];
int dp[nm];
int main(){
    #ifndef ONLINE_JUDGE
    freopen("inp.txt","r",stdin);
    #endif
    int i,j,x,y,z,w;
    IND2(k,n);
    FOR(i,1,n)
        FOR(j,1,k){
            IND(a[i][j]);
            vt[i][a[i][j]]=j;
        }
    dp[1]=1;
    FOR(i,2,k){
        dp[i]=1;
        IFOR(j,i-1,1){
            x=a[1][j];
            FOR(z,2,n)
                if( vt[z][a[1][i]]< vt[z][x]) break;
            if(z>n){
                dp[i]=max(dp[i],dp[j]+1);
            }
        }
    }
    int Max=0;
    FOR(i,1,k) if(dp[i]>Max) Max=dp[i];
    cout<<Max;
    return 0;
}

Code mẫu của ll931110

{$MODE DELPHI}
program LQDGONME;
const
  input  = '';
  output = '';
  maxn = 1000;
  maxk = 10;
var
  a,pos: array[1..maxn,1..maxk] of integer;
  F: array[1..maxn] of integer;
  n,k: integer;

procedure init;
var
  fi: text;
  i,j: integer;
begin
  assign(fi, input);
    reset(fi);

  readln(fi, n, k);
  for j := 1 to k do
    for i := 1 to n do
      begin
        read(fi, a[i,j]);
        pos[a[i,j],j] := i;
      end;

  close(fi);
end;

function valid(x,y: integer): boolean;
var
  i: integer;
begin
  valid := true;
  for i := 2 to k do if pos[x,i] > pos[y,i] then exit(false);
end;

procedure solve;
var
  u,v,i,j: integer;
begin
  for i := 1 to n do F[i] := 1;
  for i := 2 to n do
    begin
      v := a[i,1];
      for j := i - 1 downto 1 do
        begin
          u := a[j,1];
          if (F[i] < F[j] + 1) and valid(u,v) then F[i] := F[j] + 1;
        end;
    end;
end;

procedure printresult;
var
  fo: text;
  res,i: integer;
begin
  res := 0;
  for i := 1 to n do
    if res < F[i] then res := F[i];

  assign(fo, output);
    rewrite(fo);
    writeln(fo, res);
  close(fo);
end;

begin
  init;
  solve;
  printresult;
end.

Bình luận

Hãy đọc nội quy trước khi bình luận.


Không có bình luận tại thời điểm này.