📜  最长不常见子序列

📅  最后修改于: 2022-05-13 01:57:07.731000             🧑  作者: Mango

最长不常见子序列

给定两个字符串,求两个字符串的最长不常见子序列的长度。最长不常见子序列被定义为这些字符串之一的最长子序列,它不是其他字符串的子序列。
例子:

Input : "abcd", "abc"
Output : 4
The longest subsequence is 4 because "abcd"
is a subsequence of first string, but not
a subsequence of second string.

Input : "abc", "abc"
Output : 0
Both strings are same, so there is no 
uncommon subsequence.

蛮力:一般来说,有些人可能首先想到的是生成两个字符串的所有可能的 2 n个子序列,并将它们的频率存储在哈希图中。频率等于 1 的最长子序列将是所需的子序列。

C++
// CPP program to find longest uncommon
// subsequence using naive method
#include 
#include 
#include 
using namespace std;
 
// function to calculate length of longest uncommon subsequence
int findLUSlength(string a, string b)
{
    /* creating an unordered map to map
       strings to their frequency*/
    unordered_map map;
    vector strArr;
    strArr.push_back(a);
    strArr.push_back(b);
 
    // traversing all elements of vector strArr
    for (string s : strArr)
    {
        /* Creating all possible subsequences, i.e 2^n*/
        for (int i = 0; i < (1 << s.length()); i++)
        {
            string t = "";
            for (int j = 0; j < s.length(); j++) {
 
                /* ((i>>j) & 1) determines which 
                   character goes into string t*/
                if (((i >> j) & 1) != 0) 
                    t += s[j];
            }
 
            /* If common subsequence is found,
               increment its frequency*/
            if (map.count(t))
                map[t]++;
            else
                map[t] = 1;
        }
    }
    int res = 0;
    for (auto a : map) // traversing the map
    {
         // if frequency equals 1  
        if (a.second == 1)
            res = max(res, (int)a.first.length());
    }
    return res;
}
int main()
{
    // Your C++ Code
    string a = "abcdabcd", b = "abcabc"; // input strings
    cout << findLUSlength(a, b);
    return 0;
}


Java
// Java program to find longest uncommon
// subsequence using naive method
import java.io.*;
import java.util.*;
  
class GfG{
      
// function to calculate length of
// longest uncommon subsequence
static int findLUSlength(String a, String b)
{
    // creating an unordered map to map
    // strings to their frequency
    HashMap map= new HashMap();
    Vector strArr= new Vector();
    strArr.add(a);
    strArr.add(b);
 
    // traversing all elements of vector strArr
    for (String s : strArr)
    {
        // Creating all possible subsequences, i.e 2^n
        for (int i = 0; i < (1 << s.length()); i++)
        {
            String t = "";
            for (int j = 0; j < s.length(); j++) {
 
                // ((i>>j) & 1) determines which
                // character goes into string t
                if (((i >> j) & 1) != 0)
                    t += s.charAt(j);
            }
 
            // If common subsequence is found,
            // increment its frequency
            if (map.containsKey(t))
                map.put(t,map.get(t)+1);
            else
                map.put(t,1);
        }
    }
    int res = 0;
    for (HashMap.Entry entry : map.entrySet())
 
    // traversing the map
    {
        // if frequency equals 1
        if (entry.getValue() == 1)
            res = Math.max(res, entry.getKey().length());
    }
    return res;
}
 
    // Driver code
    public static void main (String[] args) {
 
    // input strings
    String a = "abcdabcd", b = "abcabc";
       System.out.println(findLUSlength(a, b));
    }
}
 
// This code is contributed by Gitanjali.


Python3
# Python3 program to find longest uncommon
# subsequence using naive method
 
# function to calculate length of
# longest uncommon subsequence
def findLUSlength(a, b):
 
    ''' creating an unordered map to map
    strings to their frequency'''
    map = dict()
    strArr = []
    strArr.append(a)
    strArr.append(b)
 
    # traversing all elements of vector strArr
    for s in strArr:
         
        ''' Creating all possible subsequences, i.e 2^n'''
        for i in range(1 << len(s)):
            t = ""
            for j in range(len(s)):
 
                ''' ((i>>j) & 1) determines which
                character goes into t'''
                if (((i >> j) & 1) != 0):
                    t += s[j]
 
            # If common subsequence is found,
            # increment its frequency
            if (t in map.keys()):
                map[t] += 1;
            else:
                map[t] = 1
 
    res = 0
    for a in map: # traversing the map
                  # if frequency equals 1
        if (map[a] == 1):
            res = max(res, len(a))
 
    return res
 
# Driver Code
a = "abcdabcd"
b = "abcabc" # input strings
print(findLUSlength(a, b))
 
# This code is contributed by Mohit Kumar


C#
// C# program to find longest
// uncommon subsequence using
// naive method
using System;
using System.Collections.Generic;
 
class GFG
{    
    // function to calculate
    // length of longest
    // uncommon subsequence
    static int findLUSlength(string a,
                             string b)
    {
        // creating an unordered
        // map to map strings to
        // their frequency
        Dictionary map =
                   new Dictionary();
        List strArr =
                 new List();
        strArr.Add(a);
        strArr.Add(b);
     
        // traversing all elements
        // of vector strArr
        foreach (string s in strArr)
        {
            // Creating all possible
            // subsequences, i.e 2^n
            for (int i = 0;
                     i < (1 << s.Length); i++)
            {
                string t = "";
                for (int j = 0;
                         j < s.Length; j++)
                {
     
                    // ((i>>j) & 1) determines
                    // which character goes
                    // into string t
                    if (((i >> j) & 1) != 0)
                        t += s[j];
                }
     
                // If common subsequence
                // is found, increment
                // its frequency
                if (map.ContainsKey(t))
                {
                    int value = map[t] + 1;
                    map.Remove(t);
                    map.Add(t, value);
                }
                else
                    map.Add(t, 1);
            }
        }
        int res = 0;
        foreach (KeyValuePair
                             entry in map)
        // traversing the map
        {
            // if frequency equals 1
            if (entry.Value == 1)
                res = Math.Max(res,
                           entry.Key.Length);
        }
        return res;
    }
     
    // Driver code
    static void Main ()
    {
         
        // input strings
        string a = "abcdabcd",
               b = "abcabc";
         
        Console.Write(findLUSlength(a, b));
    }
}
 
// This code is contributed by
// Manish Shaw(manishshaw1)


Javascript


C++
// CPP Program to find longest uncommon
// subsequence.
#include 
using namespace std;
 
// function to calculate length of longest
// uncommon subsequence
int findLUSlength(string a, string b)
{
    // Case 1: If strings are equal
    if (!a.compare(b))
        return 0;
 
     // for case 2 and case 3
    return max(a.length(), b.length());
}
 
// Driver code
int main()
{
    string a = "abcdabcd", b = "abcabc";
    cout << findLUSlength(a, b);
    return 0;
}


Java
// Java program to find longest uncommon
// subsequence using naive method
 
import java.io.*;
import java.util.*;
  
class GfG{
      
// function to calculate length of longest
// uncommon subsequence
static int findLUSlength(String a, String b)
{
    // Case 1: If strings are equal
    if (a.equals(b)==true)
        return 0;
  
     // for case 2 and case 3
    return Math.max(a.length(), b.length());
}
    // Driver code
    public static void main (String[] args) {
 
    // input strings
    String a = "abcdabcd", b = "abcabc";
       System.out.println(findLUSlength(a, b));
    }
}
 
// This code is contributed by Gitanjali.


Python3
# Python program to find
# longest uncommon
# subsequence using naive method
 
import math
 
# function to calculate
# length of longest
# uncommon subsequence
def findLUSlength( a, b):
 
    # Case 1: If strings are equal
    if (a==b) :
        return 0
  
     # for case 2 and case 3
    return max(len(a), len(b))
 
# Driver code
 
#input strings
a = "abcdabcd"
b = "abcabc"
print (findLUSlength(a, b))
 
# This code is contributed by Gitanjali.


C#
// C# program to find longest uncommon
// subsequence using naive method.
using System;
 
class GfG {
     
    // function to calculate length
    // of longest uncommon subsequence
    static int findLUSlength(String a, String b)
    {
         
        // Case 1: If strings are equal
        if (a.Equals(b)==true)
            return 0;
     
        // for case 2 and case 3
        return Math.Max(a.Length, b.Length);
    }
     
    // Driver code
    public static void Main ()
    {
 
        // input strings
        String a = "abcdabcd", b = "abcabc";
        Console.Write(findLUSlength(a, b));
    }
}
 
// This code is contributed by nitin mittal.


PHP


Javascript


输出:

8

  • 时间复杂度:O(2 x + 2 y ),其中 x 和 y 是两个字符串的长度。
  • 辅助空间:O(2 x + 2 y )。

高效算法:如果我们仔细分析问题,它似乎比看起来容易得多。所有三种可能的情况如下所述;

  1. 如果两个字符串相同,例如:“ac”和“ac”,很明显没有子序列是不常见的。因此,返回 0。
  2. 如果长度(a)=长度(b)和a? b,例如:“abcdef”和“defghi”,在这两个字符串中,一个字符串永远不会是另一个字符串的子序列。
    因此,返回长度(a)或长度(b)。
  3. 如果长度(一)?长度(b),例如:“abcdabcd”和“abcabc”,在这种情况下,我们可以将较大的字符串视为必需的子序列,因为较大的字符串不能是较小字符串的子序列。因此,返回 max(length(a), length(b))。

C++

// CPP Program to find longest uncommon
// subsequence.
#include 
using namespace std;
 
// function to calculate length of longest
// uncommon subsequence
int findLUSlength(string a, string b)
{
    // Case 1: If strings are equal
    if (!a.compare(b))
        return 0;
 
     // for case 2 and case 3
    return max(a.length(), b.length());
}
 
// Driver code
int main()
{
    string a = "abcdabcd", b = "abcabc";
    cout << findLUSlength(a, b);
    return 0;
}

Java

// Java program to find longest uncommon
// subsequence using naive method
 
import java.io.*;
import java.util.*;
  
class GfG{
      
// function to calculate length of longest
// uncommon subsequence
static int findLUSlength(String a, String b)
{
    // Case 1: If strings are equal
    if (a.equals(b)==true)
        return 0;
  
     // for case 2 and case 3
    return Math.max(a.length(), b.length());
}
    // Driver code
    public static void main (String[] args) {
 
    // input strings
    String a = "abcdabcd", b = "abcabc";
       System.out.println(findLUSlength(a, b));
    }
}
 
// This code is contributed by Gitanjali.

Python3

# Python program to find
# longest uncommon
# subsequence using naive method
 
import math
 
# function to calculate
# length of longest
# uncommon subsequence
def findLUSlength( a, b):
 
    # Case 1: If strings are equal
    if (a==b) :
        return 0
  
     # for case 2 and case 3
    return max(len(a), len(b))
 
# Driver code
 
#input strings
a = "abcdabcd"
b = "abcabc"
print (findLUSlength(a, b))
 
# This code is contributed by Gitanjali.

C#

// C# program to find longest uncommon
// subsequence using naive method.
using System;
 
class GfG {
     
    // function to calculate length
    // of longest uncommon subsequence
    static int findLUSlength(String a, String b)
    {
         
        // Case 1: If strings are equal
        if (a.Equals(b)==true)
            return 0;
     
        // for case 2 and case 3
        return Math.Max(a.Length, b.Length);
    }
     
    // Driver code
    public static void Main ()
    {
 
        // input strings
        String a = "abcdabcd", b = "abcabc";
        Console.Write(findLUSlength(a, b));
    }
}
 
// This code is contributed by nitin mittal.

PHP


Javascript


输出:

8

复杂性分析:

  • 时间复杂度:O(min(x, y)),其中 x 和 y 是两个字符串的长度。
  • 辅助空间:O(1)。