Finding pixels that make an image unique within a list, can you improve on brute force?

前端 未结 3 1838
暗喜
暗喜 2021-02-06 11:05

Suppose I have a list of strings where each string is

  • exactly 4 characters long and
  • unique within the list.

For each of these strings I wan

相关标签:
3条回答
  • 2021-02-06 11:34

    You can generate a two dimensional array which will contain the number of times each character appears in each position (0-3). For example, arr[1,3] will contain the number of times the digit/character 1 appears in the last position.

    Then for each string s, go over all characters in the string. The ones which appear only once in that position according to the array are the unique characters for that string. In other words, if arr[s[i], i]==1 Then string s is unique in position i.

    This will give you the solution in linear time, while the algorithm you gave will take quadratic time.

    0 讨论(0)
  • 2021-02-06 11:34

    If your goal is to identify images later, you could create a very fast hash of the image by picking predefined points to serve as identity pixels.

    for example, you could have a structure (class, struct, doesn't matter what language) as follows:

    structure ImageHash {
        int x_pixels, y_pixels;
        u_long hash;
        void createHash(Image img) {
            x_pixels = img.x_pixels;
            y_pixels = img.y_pixels;
            for(int i = 1; i < 5; i++) {
                int x = x_pixels / i;
                for(int j = 1; j < 5; j++) {
                    int y = y_pixels / j;
                    int r = img.getPixelRed(x,y);
                    int g = img.getPixelGreen(x,y);
                    int b = img.getPixelBlue(x,y);
                    hash = (hash * 31) ^ (r^g^b);
                }
            }
        }
    }
    

    This sort of "incomplete hash" will allow you identify possible identities, and then you can do the expensive, full comparison sparingly as required.

    Expand the incomplete hash as necessary.

    0 讨论(0)
  • 2021-02-06 11:38

    This problem can be solved by trie, or prefix tree.

    See Trie - Wikipedia, the free encyclopedia

    For the 3 strings in your example:

    abcd
    abcc
    bbcb
    

    will be turned into a trie tree (where ^ denotes the root of the tree):

    ^--a-b-c-d
     \      \
      \      c
       \
        b-b-c-b
    

    The path to the node where it branch off are the common prefix. The node after the last branch point is what makes a particular string unique. In this case, they are d, c, b.

    I assume the order of string is not important for you, that you compares all strings to find the uniqueness, not just the neighboring string.

    The complexity should be O(n x m). But this will probably affected by the domain of the characters in your string.

    0 讨论(0)
提交回复
热议问题