Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 查找列表元素间冲突相对顺序的算法_Algorithm - Fatal编程技术网

Algorithm 查找列表元素间冲突相对顺序的算法

Algorithm 查找列表元素间冲突相对顺序的算法,algorithm,Algorithm,我不知道这个问题的名称(如果我知道,我可能会查找解决方案) 问题: 给定两个整数列表,A和B,确定任何两个元素x和y之间是否存在排序冲突,其中x和y都存在于A和B中 例如,考虑这两个列表: A : 2 3 8 9 B : 3 1 7 2 在这种情况下,存在排序冲突,{2,3},因为这些元素在列表A中的顺序与在列表B中的顺序相反 琐事 A和B没有共同的元素;没有冲突 A和B是相同的列表;没有冲突 问题 有什么算法可以解决这个问题?创建一个散列(或任何快速查找数据结构),其中键作为第一个数组中的值

我不知道这个问题的名称(如果我知道,我可能会查找解决方案)

问题

给定两个整数列表,
A
B
,确定任何两个元素
x
y
之间是否存在排序冲突,其中
x
y
都存在于
A
B

例如,考虑这两个列表:

A : 2 3 8 9
B : 3 1 7 2
在这种情况下,存在排序冲突,
{2,3}
,因为这些元素在列表
A
中的顺序与在列表
B
中的顺序相反

琐事

A
B
没有共同的元素;没有冲突

A
B
是相同的列表;没有冲突

问题

有什么算法可以解决这个问题?

创建一个散列(或任何快速查找数据结构),其中键作为第一个数组中的值,值作为数组中的索引(位置)。我们把它叫做
S

def Validate(A, B):
    S = CreateLookupTable(A)
    minValueTillNow = -∞
    for i in B:
      if i in S:
         indexInA = lookup(S, i)
         if indexInA < minValueTillNow:
            return false
         minValueTillNow = indexInA

    return true
def验证(A、B):
S=CreateLookupTable(A)
minValueTillNow=-∞
对于B中的i:
如果我在S中:
indexInA=查找(S,i)
如果indexInA
不确定这是否是最优的,但以下是一种解决方法:-

  • 创建一组A&B交叉点,称之为C
  • C的元素具有属性(indexA、indexB)
  • 当我们遍历C元素时,indexA和indexB应该是递减的或递增的。如果indexA在增加,indexB在减少,那么我们有一个冲突相对顺序
  • 例如:

      A : 2 3 8 9
      B : 3 1 7 2
      C : 2(1,4),3(2,1)
    

    如我们所见,indexA在增加,而indexB在减少,因此存在冲突。

    您没有提供详细信息,但正如问题所述,可以在O(n)中完成:

    int[]A=new[]{2,8,9,10,3};
    int[]B=新[]{2,1,7,3};
    int x=2,y=3;
    INTA1=-1,a2=-1,b1=-1,b2=-1;
    对于(int i=0;i<(A.Length>B.Length?A.Length:B.Length);i++)
    {
    如果(a1==-1&&A.长度>i&&A[i]==x)a1=i;
    如果(a2==-1&&A.长度>i&&A[i]==y)a2=i;
    如果(b1==-1&&B.长度>i&&B[i]==x)b1=i;
    如果(b2==-1&&B.长度>i&&B[i]==y)b2=i;
    如果(a1>=0&&a2>=0&&b1>=0&&b2>=0)
    打破
    }
    var结果=(a1
    只需遍历数组一次,直到数组结束或所有变量都已赋值。然后,简单地检查订单是否相同

  • 获取两个列表之间的所有公共元素值

  • 创建两个列表,其中仅包含按其顺序排列的公共元素 各自的源列表(由 各自的来源清单)

  • 通过删除序号,将这些列表仅减少为无序元素 我们测试所针对的列表中的列表元素(只保留我们测试所针对的列表中的“无序”元素)

  • 数据结构做了很多工作。因此,了解它们各自的能力非常重要。简而言之,set按原样处理值,而list考虑排序和值。我们可以通过使用LinkedHashSet变得更疯狂,或者用位集做一些疯狂的事情,并添加漂亮的lambda,但希望这是非常直接的

    示例代码

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    
    public class OrderingEnforcer {
    
        public static List<Integer> getImproperlyOrderedElements(List<Integer> ordinator, List<Integer> toTest) {
    
            // create a distinct intersection of a and b (order insensitive)
            Set<Integer> intersection = new HashSet<Integer>(ordinator);
            intersection.retainAll(new HashSet<Integer>(toTest));
    
            // create a sorted list of our intersected set using the
            // indexOf(element)
            // to determine the list ordering
            List<Integer> ordinatorOrderedIntersection = new ArrayList<Integer>(intersection);
            Collections.sort(ordinatorOrderedIntersection, createIndexComparator(ordinator));
    
            List<Integer> toTestOrderedIntersection = new ArrayList<Integer>(intersection);
            Collections.sort(toTestOrderedIntersection, createIndexComparator(toTest));
    
            // Now we can create a difference of the two Lists
            toTestOrderedIntersection.removeAll(ordinatorOrderedIntersection);
            return toTestOrderedIntersection;
        }
    
        private static Comparator<Integer> createIndexComparator(List<Integer> list) {
            return (a, b) -> {
                return Integer.compare(list.indexOf(a), list.indexOf(b));
            };
        }
    }
    
    import java.util.ArrayList;
    导入java.util.Collections;
    导入java.util.Comparator;
    导入java.util.HashSet;
    导入java.util.List;
    导入java.util.Set;
    公共类排序器{
    公共静态列表GetImpropertyOrderElements(列表排序器、列表toTest){
    //创建a和b的不同交点(不区分顺序)
    集合交集=新哈希集合(序号);
    交叉点保留(新哈希集(toTest));
    //使用
    //indexOf(元素)
    //确定列表顺序的步骤
    List OrderatorOrderedIntersection=新的ArrayList(交叉点);
    排序(ordinatorOrderedIntersection,createIndexComparator(ordinator));
    List TOTESTOREDINDERSECTION=新阵列列表(交叉点);
    排序(toTestOrderedIntersection,createIndexComparator(toTest));
    //现在我们可以创建两个列表的差异
    ToToStorderedIntersection.removeAll(ordinatorOrderedIntersection);
    返回数据段;
    }
    专用静态比较器createIndexComparator(列表){
    返回(a,b)->{
    返回Integer.compare(list.indexOf(a),list.indexOf(b));
    };
    }
    }
    
    编辑:

    那么,

  • 在两个列表中创建公共元素的有序列表(按列表2排序)
  • 使用新列表,创建公共元素的有序列表(按列表1排序)也要执行相同的操作
  • 检查两个列表是否相同
  • 要创建有序列表,请在另一个列表中搜索要设置顺序的列表中的每个元素。如果找到了,请将其添加到新列表中

    这将是O(n*m)


    创建新列表时,您还必须检查不相邻的重复项。

    minValueTillNow是什么?它从未被写入?@ToddFreed打字错误。更换了左、右驾驶室。固定你不能假设列表有相同的长度,我不认为你的解决方案是有效的,因为它甚至不考虑所有元素中的任何一个列表。但是,即使它确实有效,它也只在两个列表中的公共元素正好为2和3时有效。解决方案应该找到公共元素(如有必要),而不是从硬编码的元素开始。@ToddFreed,下次是特定的
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    
    public class OrderingEnforcer {
    
        public static List<Integer> getImproperlyOrderedElements(List<Integer> ordinator, List<Integer> toTest) {
    
            // create a distinct intersection of a and b (order insensitive)
            Set<Integer> intersection = new HashSet<Integer>(ordinator);
            intersection.retainAll(new HashSet<Integer>(toTest));
    
            // create a sorted list of our intersected set using the
            // indexOf(element)
            // to determine the list ordering
            List<Integer> ordinatorOrderedIntersection = new ArrayList<Integer>(intersection);
            Collections.sort(ordinatorOrderedIntersection, createIndexComparator(ordinator));
    
            List<Integer> toTestOrderedIntersection = new ArrayList<Integer>(intersection);
            Collections.sort(toTestOrderedIntersection, createIndexComparator(toTest));
    
            // Now we can create a difference of the two Lists
            toTestOrderedIntersection.removeAll(ordinatorOrderedIntersection);
            return toTestOrderedIntersection;
        }
    
        private static Comparator<Integer> createIndexComparator(List<Integer> list) {
            return (a, b) -> {
                return Integer.compare(list.indexOf(a), list.indexOf(b));
            };
        }
    }