如何在整数数组中找到模式? [英] How can i find a pattern in an array of integers?

查看:97
本文介绍了如何在整数数组中找到模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一周前,我完成了作业,必须在C中编写一个函数. 该函数获取单个正整数数组,并且必须返回该数组中的下一个数字. 数组看起来像这样:{1,2,3,1,2,3,4,1,2,3,1,2,3,4,1,2,3,-1}; -1表示数组的结尾.

A week ago I got my homework, where I have to write a function in C. The function gets a single array of positive integers, and it has to return the next number in the array. The arrays look something like this: {1,2,3,1,2,3,4,1,2,3,1,2,3,4,1,2,3,-1}; -1 means the end of the array.

我知道该函数必须返回的数字为1,但是,如何编码模式查找算法?我还没有在互联网上找到任何解决方案,因为关于模式搜索的所有其他问题都与字符串有关,其中已经给出了必须找到的模式.

I know that the number which has to be returned by the function is 1, however, how can I code a pattern finding algorithm? I haven't found any solution on the internet, since every other question about pattern searching is with strings, where the pattern which has to be found is already given.

推荐答案

如果模式的长度为1,则对于所有可能的k,您将具有a[k+1] == a[k].

if the pattern has a length of 1, you will have a[k+1] == a[k] for all possible k.

通常,对于正确的plen(图案长度)和所有可能的k,您将具有a[k+plen] == a[k].

more generally, you will have a[k+plen] == a[k] for the correct plen (pattern length) and all possible k.

因此确定以1开头的plen ...在您的情况下,您会得到7,因为a[7]==a[0]a[8]==a[1],... a[16]==a[9],所以只需返回a[17-7].

so determine plen starting with 1... in your case you get 7 because a[7]==a[0], a[8]==a[1], ... a[16]==a[9], so just return a[17-7].

这篇关于如何在整数数组中找到模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆