Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Longest Anchored Common Sequence

  1. #1
    Junior Member
    Join Date
    Apr 2024
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Longest Anchored Common Sequence

    The longest common subsequence of strings s1, s2 is the longest string s such that s is a subsequence of both s1 and s2, i.e. we can get s by starting at some point in si, reading through, perhaps skipping some letters, and then stopping at some point.

    Suppose now our alphabet contains a special ‘anchor’ character * which binds ‘more strongly’ than ordinary letters: concretely, when reading through each si to find s we are not allowed to skip any *s (although there may be *s outside the section of si we read). So for example, suppose that s1 = A ∗ BCD∗ and s2 = B ∗ CAD. We would not be allowed to take the subsequence BC (because this requires skipping a * in s2), but we would be allowed to take the subsequence *CD, which is the longest anchored common subsequence and has length 3.

    Task: Implement Anchored.java to determine the longest anchored common subsequence (LACS) where the * character acts as an anchor that cannot be skipped. The input consists of two strings, each up to 2,000 characters long, containing letters A-Z and .

    Current Status: I've been coding this dynamic programming solution for 2 days, focusing on handling all possible edge cases involving the anchor character. Despite my efforts, my submissions are failing many hidden output cases on Codegrade.

    Challenge: The algorithm seems to perform well on standard test cases but struggles with hidden ones. I suspect there might be subtleties around how * is handled that I'm missing, or anything else.

    Request: Could anyone help me figure out what might be wrong? Any insight, especially on handling edge cases involving *, would be greatly appreciated. Here is the latest version of my code:

    import java.util.Scanner;
    import java.io.File;
    import java.io.IOException;
     
    public class Anchored {
        public static void main(String[] args) throws IOException {
            Scanner scanner = new Scanner(System.in);
            String filename = scanner.nextLine();
            File infile = new File(filename);
            Scanner input = new Scanner(infile);
            String s1 = input.nextLine();
            String s2 = input.nextLine();
            input.close();
            scanner.close();
     
            int m = s1.length();
            int n = s2.length();
            int[][] L = new int[m + 1][n + 1];
     
            for (int i = 1; i <= m; i++) {
                for (int j = 1; j <= n; j++) {
                    char c1 = s1.charAt(i - 1);
                    char c2 = s2.charAt(j - 1);
     
                    if (c1 == c2) {
                        L[i][j] = L[i - 1][j - 1] + 1;
                    } else {
                        L[i][j] = Math.max(L[i - 1][j], L[i][j - 1]);
                    }
     
                    if (c1 == '*' || c2 == '*') {
                        if (c1 == '*' && c2 == '*') {
                            L[i][j] = L[i - 1][j - 1] + 1;
                        } else if (c1 == '*') {
                            L[i][j] = L[i - 1][j];
                        } else if (c2 == '*') {
                            L[i][j] = L[i][j - 1];
                        }
                    }
                }
            }
     
            System.out.println(L[m][n]);
        }
    }

    I implemented a dynamic programming solution to calculate the longest anchored common subsequence (LACS) between two strings, where the * character acts as an anchor that must be included in the subsequence without skipping. I used a 2D array where each cell L[i][j] represents the length of the longest subsequence between the first i characters of string s1 and the first j characters of string s2. I modified the standard LCS algorithm to handle cases where one of the characters is *, ensuring that sequences containing the * were not improperly extended when mismatches occurred.

  2. #2
    Member
    Join Date
    Jan 2024
    Posts
    75
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Longest Anchored Common Sequence

    It seems like you've put a lot of effort into your implementation, which is a great start. However, I noticed a potential issue in your code related to how you handle the '*' character. Let's delve into it.

    In your code, you're updating the length of the longest subsequence based on the conditions involving '*' character correctly. However, there's one critical aspect missing: the handling of the cases where both characters are not '*'. In such cases, you need to ensure that the current character from both strings is included in the common subsequence if they match.

    Here's the part of your code that needs modification:

    ```java
    if (c1 == c2) {
    L[i][j] = L[i - 1][j - 1] + 1;
    } else {
    L[i][j] = Math.max(L[i - 1][j], L[i][j - 1]);
    }
    ```

    In the else block, you're updating `L[i][j]` based on the standard LCS algorithm, which doesn't consider the constraint imposed by the '*' character. To fix this, you should add an additional condition to ensure that when both characters are not '*', you only update `L[i][j]` if the characters match. This ensures that the sequence is extended properly while adhering to the anchor constraint.

    Here's the modified else block:

    ```java
    if (c1 == c2) {
    L[i][j] = L[i - 1][j - 1] + 1;
    } else {
    // Add an additional condition to consider non-anchor characters
    if (c1 != '*' && c2 != '*') {
    L[i][j] = Math.max(L[i - 1][j], L[i][j - 1]);
    }
    }
    ```

    With this modification, the algorithm should now correctly handle cases where both characters are not '*', ensuring that the common subsequence is extended properly while considering the anchor constraint.

    After making this modification, you can test your code again to see if it performs better on the hidden test cases. If you find yourself needing further assistance, especially with assignments related to web development, there are resources available online like ProgrammingHomeworkHelp.com assignment help platform to provide guidance and support.

Similar Threads

  1. Longest Common Subsequence using Java
    By Vithika Dutt in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: September 10th, 2023, 07:11 PM
  2. [SOLVED] Find the longest word in string - code has bugs
    By eugene3204 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: October 17th, 2020, 09:33 PM
  3. [SOLVED] Find the the longest decreasing row of numbers in a vector
    By einar123 in forum What's Wrong With My Code?
    Replies: 27
    Last Post: September 24th, 2013, 09:55 AM
  4. Binary Tree Longest Increasing Path (Recursive)
    By varsis in forum Algorithms & Recursion
    Replies: 0
    Last Post: April 13th, 2012, 10:19 PM
  5. [SOLVED] Display longest string on screen
    By mwardjava92 in forum Object Oriented Programming
    Replies: 1
    Last Post: January 29th, 2012, 12:12 PM

Tags for this Thread