This commit is contained in:
Gašper Dobrovoljc 2025-05-25 20:02:46 +02:00
parent c220ccf97c
commit 3655eaab86
No known key found for this signature in database
GPG Key ID: 0E7E037018CFA5A5
7 changed files with 141 additions and 0 deletions

29
naloga5/.gitignore vendored Normal file
View File

@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

10
naloga5/.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,10 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Environment-dependent path to Maven home directory
/mavenHomeManager.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,19 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyPep8Inspection" enabled="true" level="INFORMATION" enabled_by_default="true">
<option name="ignoredErrors">
<list>
<option value="W292" />
</list>
</option>
</inspection_tool>
<inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoredIdentifiers">
<list>
<option value="tests.smoke.test_absences_sliding.TestAbsencesSliding.*" />
</list>
</option>
</inspection_tool>
</profile>
</component>

6
naloga5/.idea/misc.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_24" default="true" project-jdk-name="homebrew-23" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
naloga5/.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/naloga5.iml" filepath="$PROJECT_DIR$/naloga5.iml" />
</modules>
</component>
</project>

11
naloga5/naloga5.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

58
naloga5/src/Naloga5.java Normal file
View File

@ -0,0 +1,58 @@
import java.util.ArrayList;
public class Naloga5 {
static String[][] getEditDistanceAlignments(String s1, String s2) {
int m = s1.length();
int n = s2.length();
int[][] dp = new int[m + 1][n + 1];
for (int i = 0; i <= m; i++) dp[i][0] = i;
for (int j = 0; j <= n; j++) dp[0][j] = j;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = 1 + Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i][j - 1]));
}
}
}
ArrayList<String[]> result = new ArrayList<>();
backtrack(s1, s2, m, n, "", "", dp, result);
String[][] alignments = new String[result.size()][2];
for (int i = 0; i < result.size(); i++) {
alignments[i][0] = result.get(i)[0];
alignments[i][1] = result.get(i)[1];
}
return alignments;
}
static void backtrack(String s1, String s2, int i, int j, String aligned1, String aligned2, int[][] dp, ArrayList<String[]> result) {
if (i == 0 && j == 0) {
result.add(new String[]{new StringBuilder(aligned1).reverse().toString(), new StringBuilder(aligned2).reverse().toString()});
return;
}
if (i > 0 && j > 0 && s1.charAt(i - 1) == s2.charAt(j - 1) && dp[i][j] == dp[i - 1][j - 1]) {
backtrack(s1, s2, i - 1, j - 1, aligned1 + s1.charAt(i - 1), aligned2 + s2.charAt(j - 1), dp, result);
}
if (i > 0 && j > 0 && dp[i][j] == dp[i - 1][j - 1] + 1) {
backtrack(s1, s2, i - 1, j - 1, aligned1 + s1.charAt(i - 1), aligned2 + s2.charAt(j - 1), dp, result);
}
if (i > 0 && dp[i][j] == dp[i - 1][j] + 1) {
backtrack(s1, s2, i - 1, j, aligned1 + s1.charAt(i - 1), aligned2 + '-', dp, result);
}
if (j > 0 && dp[i][j] == dp[i][j - 1] + 1) {
backtrack(s1, s2, i, j - 1, aligned1 + '-', aligned2 + s2.charAt(j - 1), dp, result);
}
}
}