Editorial for Xâu con chung dài nhất
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
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 max=1000000; var n,m,i,j,re,t:longint; a,b:array[1..max] of char; c:array[0..1,0..max] of longint; begin {---------Read---------} n:=0; while not eoln do begin inc(n); read(a[n]); end; readln; m:=0; while not eoln do begin inc(m); read(b[m]); end; {---------Process---------} re:=0; fillchar(c,sizeof(c),0); for i:=1 to n do for j:=1 to m do begin t:=i mod 2; if c[1-t,j]>=c[t,j-1] then c[t,j]:=c[1-t,j] else c[t,j]:=c[t,j-1]; if (a[i]=b[j]) and (c[t,j]<=c[1-t,j-1]) then begin c[t,j]:=c[1-t,j-1]+1; if c[t,j]>re then re:=c[t,j]; end; end; {---------Write---------} write(re); end.
Code mẫu của happyboy99x
#include <algorithm> #include <bitset> #include <cctype> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef pair<int, int> ii; typedef vector<ii> vii; typedef vector<vii> vvii; typedef vector<int> vi; typedef vector<vi> vvi; #define sz(a) int((a).size()) #define fi first #define se second #define pb push_back #define mp make_pair #define all(c) (c).begin(), (c).end() #define tr(c,i) for(typeof((c).begin()) i = (c).begin(), _e = (c).end(); i != _e; ++i) #define present(c,x) ((c).find(x) != (c).end()) #define cpresent(c,x) (find(all(c),x) != (c).end()) #define rep(i,n) for(int i = 0, _n = (n); i < _n; ++i) #define repd(i,n) for(int i = (n)-1; i >= 0; --i ) #define fo(i,a,b) for(int i = (a), _b = (b); i <= _b; ++i) #define fod(i,a,b) for(int i = (a), _b = (b); i >= _b; --i) #define INF 1000000000 #define N 5000+5 int f[N][N]; char x[N], y[N]; int main() { gets(x+1); gets(y+1); fo(i,1,strlen(x+1)) fo(j,1,strlen(y+1)) f[i][j] = x[i] == y[j] ? f[i-1][j-1]+1 : max(f[i-1][j],f[i][j-1]); printf("%d\n", f[strlen(x+1)][strlen(y+1)]); return 0; }
Code mẫu của ladpro98
{$H+} program dayconchungdainhat1; uses math; var s1,s2:string; i,j:word; f:array[0..1000,0..1000] of longint; begin readln(s1); readln(s2); for i:=1 to length(s1) do f[i,0]:=0; for i:=1 to length(s2) do f[0,i]:=0; for i:=1 to length(s1) do for j:=1 to length(s2) do begin if s1[i]=s2[j] then f[i,j]:=f[i-1,j-1]+1 else f[i,j]:=max(f[i,j-1],f[i-1,j]); end; write(f[i,j]); end.
Code mẫu của RR
uses math; const MAXN=2011; var a,b:ansistring; m,n:longint; f:array[0..MAXN,0..MAXN] of longint; i,j:longint; begin readln(a); m:=length(a); readln(b); n:=length(b); for i:=1 to m do for j:=1 to n do if a[i]=b[j] then f[i,j]:=f[i-1,j-1]+1 else f[i,j]:=max(f[i-1,j],f[i,j-1]); writeln(f[m,n]); end.
Code mẫu của hieult
#include <stdio.h> //#include <conio.h> #include <string.h> int f[1001][1001]; int max(int a,int b) { if(a>b) return a; else return b; } int main() { // freopen("QBSTR.in","r",stdin); char s1[10001],s2[10001]; scanf("%s %s",s1,s2); int n = strlen(s1),m = strlen(s2); if(s1[0] == s2[0]) f[0][0]=1; else f[0][0]=0; for(int i = 1;i<=m;i++) { if(s1[0] == s2[i]) f[0][i]=1 ; else f[0][i] = f[0][i-1]; } for(int i = 1;i<=n;i++) { if(s1[i] == s2[0]) f[i][0]=1 ; else f[i][0] = f[i-1][0]; } for(int i = 1;i<n;i++) for(int j = 1;j<m;j++) { if(s1[i]==s2[j]) f[i][j]=f[i-1][j-1]+1; else f[i][j] = max(f[i-1][j],f[i][j-1]); // printf("%d %d %d \n",i,j,f[i][j]); } printf("%d",f[n-1][m-1]); // getch(); }
Code mẫu của ll931110
Program QBSTR; Const input = ''; output = ''; Var x,y: string; m,n: integer; F: array[0..1000,0..1000] of integer; Procedure init; Var fi: text; i: integer; Begin Assign(fi, input); Reset(fi); Readln(fi, x); Readln(fi, y); Close(fi); m:= length(x); n:= length(y); Fillchar(F, sizeof(F), 0); End; Function max(x,y: integer): integer; Begin If x < y then max:= y else max:= x; End; Procedure optimize; Var i,j: integer; Begin For i:= 1 to m do For j:= 1 to n do If x[i] = y[j] then F[i,j]:= F[i - 1, j - 1] + 1 else F[i,j]:= max(F[i - 1,j], F[i,j - 1]); End; Procedure printresult; Var fo: text; Begin Assign(fo, output); Rewrite(fo); Writeln(fo, F[m,n]); Close(fo); End; Begin init; optimize; printresult; End.
Code mẫu của skyvn97
#include<stdio.h> #include<string.h> #define MAX 1000 char a[MAX]; char b[MAX]; int opt[MAX][MAX]; int i,j; int max(int x,int y,int z) { int min=x; if (min<y) min=y; if (min<z) min=z; return (min); } int main(void) { gets(a); gets(b); for (i=0;i<strlen(a);i=i+1) for (j=0;j<strlen(b);j=j+1) { if (a[i]==b[j]) opt[i+1][j+1]=opt[i][j]+1; else opt[i+1][j+1]=max(opt[i][j],opt[i][j+1],opt[i+1][j]); } printf("%d",opt[strlen(a)][strlen(b)]); }
Code mẫu của khuc_tuan
#include <iostream> using namespace std; char a[2200], b[2200]; int f[2200][2200]; int main() { gets(a); gets(b); int m = strlen(a); int n = strlen(b); for(int i=0;i<=m;++i) for(int j=0;j<=n;++j) if(i==0 || j==0) f[i][j] = 0; else f[i][j] = max(max( f[i][j-1], f[i-1][j]), (a[i-1]==b[j-1]) ? (f[i-1][j-1]+1) : 0); cout << f[m][n] << endl; return 0; }
Comments