#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define per(i, a, b) for(int i=(a-1); i>=(b); i--)
#define qrep(i, q) for(auto i : q)
#define sz(a) (int)a.size()
#define de(a) cout<<#a<<" = "<<a<<endl
#define dd(a) cout<<#a<<" = "<<a<<" "
#define lowbit(x) x&(-x)
#define all(x) x.begin(),x.end()
#define endl "\n"
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int maxn = 1005;
const int logn = (log(maxn)/log(2))+5;
const int inf = 0x3f3f3f3f;
const ld eps = 1e-9;
char a[maxn],b[maxn];
int dp[maxn][maxn];
int solve(int x = -1)
{
//std::ios::sync_with_stdio(false);
//std::cin.tie(0);
if(x != -1){
char infile[10] = "0.in";
char outfile[10] = "0.out";
infile[0] = outfile[0] = '0'+x;
freopen(infile, "r", stdin);
freopen(outfile, "w", stdout);
}
scanf("%s%s", a+1, b+1);
int n = strlen(a+1);
int m = strlen(b+1);
memset(dp, 0, sizeof dp);
rep(i, 1, n+1){
rep(j, 1, m+1){
if(a[i] == b[j]) dp[i][j] = dp[i-1][j-1]+1;
else dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
}
}
cout << dp[n][m] << endl;
if(x != -1){
fclose(stdin);
fclose(stdout);
}
return 0;
}
int main(){
//for(int i = 0;i < 10;i++) solve(i);
solve();
}
辉夜大小姐——算法题c转java
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- 程序员星辰 2023-01-31 10:41关注
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintStream; import java.util.Scanner; public class Main { static int maxn = 1005; static int logn = (int)(Math.log(maxn) / Math.log(2)) + 5; static int inf = 0x3f3f3f3f; static double eps = 1e-9; static char a[] = new char[maxn]; static char b[] = new char[maxn]; static int dp[][] = new int[maxn][maxn]; public static int solve(int x) { if (x != -1) { String infile = "0.in"; String outfile = "0.out"; infile = "" + (char)('0' + x); outfile = "" + (char)('0' + x); try { System.setIn(new FileInputStream(infile)); System.setOut(new PrintStream(outfile)); } catch (FileNotFoundException e) { e.printStackTrace(); } } Scanner sc = new Scanner(System.in); String sa = sc.nextLine(); String sb = sc.nextLine(); int n = sa.length(); int m = sb.length(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (sa.charAt(i-1)==sb.charAt(j-1)) dp[i][j] = dp[i] [j-1] + 1; else dp[i][j] = Math.max(dp[i][j-1], dp[i-1][j]); } } System.out.println(dp[n][m]); if (x != -1) { try { System.in.close(); System.out.close(); } catch (IOException e) { e.printStackTrace(); } } return 0; } public static void main(String[] args) { solve(-1); } }
解决 无用评论 打赏 举报
悬赏问题
- ¥15 程序不包含适用于入口点的静态Main方法
- ¥15 素材场景中光线烘焙后灯光失效
- ¥15 请教一下各位,为什么我这个没有实现模拟点击
- ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
- ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
- ¥20 有关区间dp的问题求解
- ¥15 多电路系统共用电源的串扰问题
- ¥15 slam rangenet++配置
- ¥15 有没有研究水声通信方面的帮我改俩matlab代码
- ¥15 ubuntu子系统密码忘记