Chủ Nhật, 24 tháng 4, 2022

XCLN C++

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

string a, b;
int n, m;
int f[1005][1005];
int x, y;
string res = "";
void nhap()
{
    getline(cin,a);
    getline(cin,b);
    n = a.size();
    m = b.size();
    a = ' ' + a;
    b = ' ' + b;
}

bool ss(string x,string y)
{
    while(x.size()<y.size()) x="0"+x;
    while(y.size()<x.size()) y="0"+y;
    if(x>y) return true;
    else return false;
}
void giai(){
    for (int i=1;i<=n;i++)
    {
        for (int j=1;j<=m;j++)
        {
            if(a[i]==b[j]&&'0'<=a[i]&&a[i]<='9')
                f[i][j]=f[i-1][j-1]+1;
            string s="";
            for(int k=1;k<=f[i][j];k++)
            {
                s=a[i+1-k]+s;
            }
            if(s!="")
            //cout<<i<<" "<<j<<" "<<s<<endl;

            if (ss(s,res)==true)
            {
                res = s;
            }
        }
    }
    cout<<res<<endl;
  //  cout<<x+1<<" "<<y+1;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
   // freopen("cau3.inp", "r", stdin);
  //  freopen("cau3.out", "w", stdout);
    nhap();
    giai();
}

Không có nhận xét nào:

Đăng nhận xét

Lưu ý: Chỉ thành viên của blog này mới được đăng nhận xét.