0% found this document useful (0 votes)
88 views25 pages

Tai Lieu Code

The document contains code to find the longest common subsequence between two strings. It defines a function that takes in two strings, declares a 2D array to store the LCS values, initializes the array, calculates the LCS values using a recursive formula, backtracks to find the actual subsequence, and returns the length and subsequence. It uses dynamic programming by building the solution from optimal solutions to overlapping subproblems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views25 pages

Tai Lieu Code

The document contains code to find the longest common subsequence between two strings. It defines a function that takes in two strings, declares a 2D array to store the LCS values, initializes the array, calculates the LCS values using a recursive formula, backtracks to find the actual subsequence, and returns the length and subsequence. It uses dynamic programming by building the solution from optimal solutions to overlapping subproblems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

#include<iostream>

#include<string.h>
using namespace std;
char C[100];
int nC = 0;
int NP[100];
int kt = -1;
bool laChuoiCon(char B[], int nB, char C[], int nC);
void xauchung(char A[], int nA, int NP[], int n, char B[], int nB, unsigned long long& kq);
unsigned long long charToInt(char C[], int nC);
int main()
{
char X[100];
char Y[100];
gets(X);
gets(Y);
int nX = strlen(X);
int nY = strlen(Y);
unsigned long long kq = 0;
xauchung(X, nX, NP, 0, Y, nY, kq);
if (kt == 0)

1
cout << kq;
else
cout << -1;

return 0;
}
bool laChuoiCon(char B[], int nB, char C[], int nC)
{
int count = 0;
int x = 0;
for (int i = 0; i < nB; i++)
{
if (B[i] == C[count])
count++;
if (count == nC)
return true;
}
return false;
}

unsigned long long charToInt(char C[], int nC)


{
unsigned long long sum = 0;
for(int i =0; i< nC; i++)
sum = sum*10 + ((int)C[i] - 48);
return sum;
}

void xauchung(char A[], int nA, int NP[], int n, char B[], int nB, unsigned long long& kq)
{

2
if (n == nA)
{
for (int i = 0; i < nA; i++)
{
if (NP[i] == 1)
C[nC++] = A[i];d
}
if (laChuoiCon(B,nB, C,nC) && nC != 0)
{
kt = 0;
unsigned long long sum = charToInt(C,nC);
if (kq < sum)
kq = sum;
}
nC = 0;
return;
}

for (int i = 0; i <= 1; i++)


{
NP[n] = i;
xauchung(A, nA, NP, n + 1, B, nB, kq);
}
}

3
#include<iostream>
#include<cmath>
using namespace std;
bool istrue(int A[][102], int i, int j, int k);
int thoaduoc(int A[][102], int m, int n, int k);
int main()
{
int A[102][102] ={0}, m, n;
int k;
cin >> m >> n >> k;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
cin >> A[i][j];
int kq = thoaduoc(A, m, n, k);
cout << kq;
return 0;
}

4
int thoaduoc(int A[][102], int m, int n, int k)
{
int dem = 0;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
if (istrue(A, i, j, k))
{
dem++;
}
return dem;
}

bool istrue(int A[][102], int i, int j, int k)


{
int dem = 0;
int tong = 0;
for(int x = i-1; x<=i+1; x++)
for (int y = j - 1; y <= j + 1; y++)
{
if (!(x == i && y == j))
{
tong += A[x][y];
if (A[x][y] != 0)
dem++;
}

}
float kq = (float)tong / dem;
float t = abs(kq - A[i][j]);

5
if (t < k)
{
return true;
}
return false;
}

#include <iostream>

using namespace std;

int main()
{
int n,t=0,f[10001]={0},d[10001];
cin>>n;
int a[n+1];
for (int i=1;i<=n;i++)
{
cin>>a[i];
t=t+a[i];
}
t=t/2;

6
//QHD
for (int i=1;i<=t;i++)
{
f[i]=INT_MAX;
for (int j=1;j<=n;j++)
if (i>=a[j] && j>f[i-a[j]])
{
f[i]=j;
break;
}
}
//Truyvet
while (f[t] > n) t--;
while (t > 0)
{
d[f[t]]=1;
t=t-a[f[t]];
}
int tong1=0;
for (int i=1;i<=n;i++)
if(d[i]==1)
tong1=tong1+a[i];
int tong2=0;
for (int i=1;i<=n;i++)
if (d[i]!=1)
tong2=tong2+a[i];
cout<<abs(tong1-tong2);
}

7
#include<bits/stdc++.h>
using namespace std;

string Sum(string a, string b){


while (a.size()<b.size()) a = "0" + a;
while (a.size()>b.size()) b = "0" + b;
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
string result = "";

int du = 0;
int tong = 0;
for(int i=0; i< a.size(); i++){
tong = (a[i]-'0')+(b[i]-'0')+du;
du=tong/10;
result+=(char)('0'+tong%10);
}
if(du) result+="1";
reverse(result.begin(), result.end());
return result;

8
}

string solve(int n){


if(n==0) return "0";
else if (n==1) return "1";
string a= "0";
string b= "1";
string c= "";
for(int i=2; i<=n; i++){
c=Sum(a, b);
a=b;
b=c;
}
return c;
}

int main()
{
int n;
cin>>n;
cout<<solve(n);
}

9
#include<iostream>
using namespace std;
int xoayBit(int n, int k);
int main()
{
int n,k;
cin>>n>>k;
cout<<xoayBit(n,k);
}
int xoayBit(int n, int k)
{
int x = n;
n>>=(32-k);
int kBit1Cuoi = ~((0x1<<31)>>(31-k));
n&=kBit1Cuoi;
x<<=k;
int sum = n|x;
return sum;
}

#include<iostream>

10
using namespace std;
int duongdi(int A[][8], int xy[][2], int n, int x, int y);
int main()
{
int n = 8, A[8][8];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
A[i][j] = 0;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
if (x1 < 0 || x2 < 0 || x1 >= n || y1 >= n || y2 >= n || y1 < 0 || y2 < 0) {
cout << 0; return 0;
}
int xy [1][2], huong; xy[0][0] = x2, xy[0][1] = y2;;
cout<<duongdi(A, xy, n, x1, y1);
return 0;
}
int duongdi(int A[][8],int xy[][2], int n ,int x, int y)
{
int X[8] = { -2, -2,-1,1,2,2,1,-1 };
int Y[8] = { 1,-1,-2,-2,-1,1,2,2 };
for (int i = 0; i < 8; i++)
{
int xnext = x + X[i], ynext = y + Y[i];
if (xnext >= 0 && xnext < n && ynext >= 0 && ynext < n)
{
if (xnext == xy[0][0] && ynext == xy[0][1])
return i+1;
}
}

11
return 0;
}

#include<iostream>
#define MAX 100
using namespace std;
int A[MAX],B[MAX],n,k,summax;
void lietkeNP(int k);
void xuat();
void tinh();
int main()
{
cin>>n>>k;
for(int i=0;i<n;i++)
cin>>A[i];
lietkeNP(0);
cout<<summax;
return 0;
}
void lietkeNP(int k)
{
if(k==n)
tinh();
else
{
12
for(int i=0;i<=1;i++)
{
B[k]=i;
lietkeNP(k+1);
}
}
}
void xuat()
{
for(int i=0;i<n;i++)
cout<<A[i]<<" ";
cout<<endl;
}
void tinh()
{
int sum=0;
for(int i=0;i<n;i++)
if(B[i]==1) sum+=A[i];
if(sum%k==0)
if(sum>summax)
summax=sum;

13
#include<bits/stdc++.h>
using namespace std;
#define MAX 100

int BT(long long M)


{
long long F[MAX];
F[0]=2;
F[1]=4;
F[2]=6;
for(int i=3; i<MAX; i++){
F[i]=2*F[i-3]+4*(F[i-2])+6*(F[i-1]);
}
for(int i=0; i<MAX; i++){
if(F[i]>M)
return i-1;
}
}

int main()
{
int n;
cin>>n;
cout<<BT(n);
}

14
#include <bits/stdc++.h>

using namespace std;

int main()
{
int n,t=0,f[10001]={0},d[10001];
cin>>n;
int a[n+1];
for (int i=1;i<=n;i++)
{
cin>>a[i];
t=t+a[i];
}
t=t/2;
//QHD
for (int i=1;i<=t;i++)
{

15
f[i]=INT_MAX;
for (int j=1;j<=n;j++)
if (i>=a[j] && j>f[i-a[j]])
{
f[i]=j;
break;
}
}
//Truyvet
while (f[t] > n) t--;
while (t > 0)
{
d[f[t]]=1;
t=t-a[f[t]];
}
int tong1=0;
for (int i=1;i<=n;i++)
if(d[i]==1)
tong1=tong1+a[i];
int tong2=0;
for (int i=1;i<=n;i++)
if (d[i]!=1)
tong2=tong2+a[i];
cout<<abs(tong1-tong2);
}

16
#include<iostream>
#include<string> // Thư viện xử lý xâu
using namespace std;

void longest_Common(string a, string b){ // Hàm tìm xâu con lớn nhất và in ra màn hình
int n = a.size(); // n chiều dài xâu a, m chiều dài xâu b
int m = b.size();
int max_Size; // Biến lưu độ dài con chung lớn nhất
string subsequence = ""; // Biến lưu con chung dùng khi truy vết
int L[n+1][m+1]; // Khai báo mảng lưu kết quả: n+1 hàng, m+1 cột

for(int i=0; i<=n; i++) // Gán cột đầu tiên bằng 0


L[i][0] = 0;
for(int j=0; j<=m; j++) // Gán hàng đầu tiên = 0
L[0][j] = 0;

for(int i = 1; i<=n; i++){


for(int j = 1; j<=m; j++){
if(a[i-1] == b[j-1]){ // Nếu có phần tử bằng nhau

17
L[i][j] = L[i-1][j-1] + 1; // Áp dụng công thức
}
else{ // Trường hợp a[i-1] khác b[j-1]
if(L[i-1][j] >= L[i][j-1]) // Tìm max giữa L[i-1][j] và L[i][j-1]
L[i][j] = L[i-1][j];
else
L[i][j] = L[i][j-1];
}
}
}

max_Size = L[n][m]; // Tìm được độ dài con lớn nhất


int i = n;
int j = m;
while(L[i][j] != 0){ // Điều kiện dừng
if(a[i-1] == b[j-1]){ // Nếu bằng nhau
subsequence += a[i-1]; // Cộng a[i-1] vào xâu con
i--;
j--;
}
else{ // Nếu khác nhau
if(L[i-1][j] >= L[i][j-1]) // So sánh
i--;
else
j--;
}
}

cout<<max_Size; // In ra độ dài con lớn nhất


}

18
int main(){
string a, b;
cin>>a;
cin>>b;
longest_Common(a,b);
return 0;
}

#include <iostream>
using namespace std;

int main()
{
string myStr;
getline(cin, myStr);
//cout<<myStr;
int n = myStr.size();
int i, j = 0;

for(i = 0; i < n; i++) {

19
if(myStr[i] == ' ') {
for(int pos = i-1; pos >= j; pos--) cout << myStr[pos];
j = i+1;
cout << " ";
}
if(i == n-1) {
for(int pos = i; pos >= j; pos--) cout << myStr[pos];
}
}
}

#include<bits/stdc++.h>
using namespace std;

int main()
{
char S[10000];
int n;
cin>>n;
for(int i=1; i<=n; i++){
itoa(i,S,2);
puts(S);
}

20
}

#include<bits/stdc++.h>
using namespace std;
int tinhDoSau();
void lietKeNP(int k);
void xuat();
int m,n;
int dem=0;
int A[100];
int main(){
cin>>m>>n;
lietKeNP(0);
//cout<<dem;
return 0;
}
void lietKeNP(int k){
if(k==m){
if(tinhDoSau()==n)

21
xuat();
}
else
for(int i=0; i<=1; i++)
{
A[k]=i;
lietKeNP(k+1);
}
}
void xuat()
{
dem++;
for(int i=0; i<m; i++)
if(A[i]==0)
cout<<'(';
else
cout<<')';
cout<<endl;
}
int tinhDoSau(){
int i=0;
int ngoacmo=0;
int dosau=0;
while(i<m){
if(A[i]==0)
ngoacmo++;
else
{
if(ngoacmo==0)
return -1;

22
if(ngoacmo>dosau)
dosau=ngoacmo;
ngoacmo--;
}
i++;
}
if(ngoacmo==0)
return dosau;
else
return -1;
}
CỘNG TRỪ SỐ LỚN
#include<bits/stdc++.h>
using namespace std;
string Sum(string a, string b) {
while (a.size() < b.size()) a = "0" + a;
while (a.size() > b.size()) b = "0" + b;
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
string result = "";

int du = 0;
int tong = 0;
for (int i = 0; i < a.size(); i++){
tong = (a[i] - '0') + (b[i] - '0') + du;
du = tong / 10;
result += (char)('0' + tong % 10);
}
if (du) result += "1";
reverse(result.begin(), result.end());

23
return result;
}
string Sub(string a, string b) {
while (a.size() < b.size()) return Sub(b,a);
while (a.size() > b.size()) b = "0" + b;
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
string result = "";

int nho = 0;
int hieu = 0;
for (int i = 0; i < a.size(); i++) {
hieu = (a[i] - '0') - (b[i] - '0') - nho;
if (hieu < 0) {
hieu = 10 + (a[i] - '0') - (b[i] - '0') - nho;
nho = 1;
}
else
nho = 0;
if(i<a.size())
result += (char)('0' + hieu % 10);
}
if (nho) {
result += (char)('1' + hieu % 10);
}
reverse(result.begin(), result.end());
while (result[0] == '0') {
for (int i = 0; i < result.size(); i++)
{
result[i] = result[i + 1];

24
}
result[result.size()] = '\0';
}
return result;
}
string solve(int n) {
if (n == 0) return "0";
else if (n == 1)return "1";
string a = "0";
string b = "1";
string c = "1";
for (int i = 2; i <= n; i++) {
c = Sum(a, b);
a = b;
b = c;
}
return c;
}
int main()
{
string a, b;
cin >> a >> b;
cout << Sum(a,b);
}

25

You might also like