序列(字符串)包括以滑动方式重复另一个序列

时间:2019-06-09 06:17:52

标签: arrays string algorithm sequence

我有两个数组,但是将使用字符串使说明更清楚。需要找出第一个字符串是滑动重复还是第二个字符串。示例(管道仅用于视觉定界,您可能会认为它们不存在):

Second string is 'abcd'
'abcd|abcd' - yes
'abcd' - yes
'bcd|abcd|ab' - yes
'cd|abc' - yes
'd|a' - yes
'ab' - yes
'сd' - yes

好的,将尝试给出另一个解释以使其更清楚。我们有需要检查的第一个字符串,第二个是模式。我们重复图案无数次。如果第一个字符串是我们无休止模式的子字符串,那么答案是肯定的,否则否定...好吧,似乎这种额外的解释为解决方案提供了很好的提示!

1 个答案:

答案 0 :(得分:0)

给出我的额外解释后,遇到以下解决方案。函数indexOf(...)取自java.lang.String的源代码,并被int[]而非char[]所采用(真的很琐碎,不值得解释)。

    /**
     * Examines if one sequence is a repeating of the other sequence (pattern) in sliding manner.
     * Given are: a sequence that we need to examine, and a pattern. Repeat the pattern infinite number of times.
     * After that if the first sequence is a subsequence of ours endless pattern then the answer is yes, otherwise no.
     * 
     * @param where sequence to be examined
     * @param what pattern
     * @return position in the {@code where} where the pattern starts. Can be outside the {@code where}. See unit 
     *         tests for more details. 
     */
    public static int indexOfSliding(int[] where, int[] what) {

        int multiplier = (int) (Math.ceil((double) where.length / what.length)) + 1;
        int[] biggerWhat = new int[what.length * multiplier];
        for (int i = 0; i < multiplier; i++) 
            for (int j = 0; j < what.length; j++)
                biggerWhat[i * what.length + j] = what[j % what.length];

        int result = indexOf(biggerWhat, where);

        if (result == -1) {
            return -1;
        } else {
            return result == 0 ? 0 : what.length - result;
        }
    }

    @Test
    public void testIndexOfSliding() {

        assertEquals(0, Utils.indexOfSliding(new int[] {}, new int[] { 1 }));
        assertEquals(-1, Utils.indexOfSliding(new int[] { 1 }, new int[] {}));
        assertEquals(0, Utils.indexOfSliding(new int[] {}, new int[] {}));

        assertEquals(0, Utils.indexOfSliding(new int[] { 1 }, new int[] { 1 }));
        assertEquals(-1, Utils.indexOfSliding(new int[] { 1 }, new int[] { 2 }));

        assertEquals(0, Utils.indexOfSliding(new int[] { 1, 1 }, new int[] { 1 }));
        assertEquals(-1, Utils.indexOfSliding(new int[] { 1, 1 }, new int[] { 2 }));

        assertEquals(0, Utils.indexOfSliding(new int[] { 1, 2 }, new int[] { 1, 2 }));
        assertEquals(1, Utils.indexOfSliding(new int[] { 2, 1 }, new int[] { 1, 2 }));
        assertEquals(0, Utils.indexOfSliding(new int[] { 1, 2, 1 }, new int[] { 1, 2 }));
        assertEquals(0, Utils.indexOfSliding(new int[] { 1, 2, 1, 2 }, new int[] { 1, 2 }));
        assertEquals(0, Utils.indexOfSliding(new int[] { 1, 2, 1, 2, 1 }, new int[] { 1, 2 }));
        assertEquals(1, Utils.indexOfSliding(new int[] { 2, 1, 2, 1 }, new int[] { 1, 2 }));
        assertEquals(1, Utils.indexOfSliding(new int[] { 2, 1, 2, 1, 2, 1 }, new int[] { 1, 2 }));
        assertEquals(-1, Utils.indexOfSliding(new int[] { 1, 0 }, new int[] { 1, 2 }));
        assertEquals(-1, Utils.indexOfSliding(new int[] { 1, 1 }, new int[] { 1, 2 }));

        assertEquals(0, Utils.indexOfSliding(new int[] {1, 2, 3 }, new int[] { 1, 2, 3 }));
        assertEquals(0, Utils.indexOfSliding(new int[] {1, 2, 3, 1, 2, 3 }, new int[] { 1, 2, 3 }));
        assertEquals(1, Utils.indexOfSliding(new int[] { 3, 1, 2, 3 }, new int[] { 1, 2, 3 }));
        assertEquals(2, Utils.indexOfSliding(new int[] { 2, 3, 1, 2 }, new int[] { 1, 2, 3 }));
        assertEquals(2, Utils.indexOfSliding(new int[] { 2, 3, 1, 2, 3 }, new int[] { 1, 2, 3 }));
        assertEquals(2, Utils.indexOfSliding(new int[] { 2, 3, 1, 2, 3, 1 }, new int[] { 1, 2, 3 }));
        assertEquals(-1, Utils.indexOfSliding(new int[] { 3, 2, 1, 2, 3, 1 }, new int[] { 1, 2, 3 }));

        // Pattern bigger than examined sequence, pattern size - 2
        assertEquals(0, Utils.indexOfSliding(new int[] { 1 }, new int[] { 1, 2 }));
        assertEquals(1, Utils.indexOfSliding(new int[] { 2 }, new int[] { 1, 2 }));
        assertEquals(-1, Utils.indexOfSliding(new int[] { 0 }, new int[] { 1, 2 }));

        // Pattern bigger than examined sequence, pattern size - 3
        assertEquals(0, Utils.indexOfSliding(new int[] { 1 }, new int[] { 1, 2, 3 }));
        assertEquals(2, Utils.indexOfSliding(new int[] { 2 }, new int[] { 1, 2, 3 }));
        assertEquals(1, Utils.indexOfSliding(new int[] { 3 }, new int[] { 1, 2, 3 }));
        assertEquals(-1, Utils.indexOfSliding(new int[] { 0 }, new int[] { 1, 2, 3 }));
        assertEquals(0, Utils.indexOfSliding(new int[] { 1, 2 }, new int[] { 1, 2, 3 }));
        assertEquals(2, Utils.indexOfSliding(new int[] { 2, 3 }, new int[] { 1, 2, 3 }));
        assertEquals(1, Utils.indexOfSliding(new int[] { 3, 1 }, new int[] { 1, 2, 3 }));
        assertEquals(-1, Utils.indexOfSliding(new int[] { 1, 3 }, new int[] { 1, 2, 3 }));
        assertEquals(-1, Utils.indexOfSliding(new int[] { 0, 1 }, new int[] { 1, 2, 3 }));

    }