Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.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
查找字符串java中的递归模式_Java_String_Algorithm_Data Structures_Pattern Matching - Fatal编程技术网

查找字符串java中的递归模式

查找字符串java中的递归模式,java,string,algorithm,data-structures,pattern-matching,Java,String,Algorithm,Data Structures,Pattern Matching,在我的一次采访中,我问了一个关于java字符串的程序,我无法回答。我不知道这是一个简单的程序还是复杂的程序。我在互联网上搜索过,但找不到确切的解决办法。我的问题是, 我假设一个字符串包含递归模式,比如 String str1 = "abcabcabc"; 在上面的字符串中,递归模式是“abc”,它在一个字符串中重复,因为这个字符串只递归地包含“abc”模式 如果我将此字符串作为参数传递给函数/方法,则函数/方法应返回“此字符串具有递归模式”。如果该字符串没有任何递归模式,则函数/方法应返回“此

在我的一次采访中,我问了一个关于java字符串的程序,我无法回答。我不知道这是一个简单的程序还是复杂的程序。我在互联网上搜索过,但找不到确切的解决办法。我的问题是,

我假设一个字符串包含递归模式,比如

String str1 = "abcabcabc";
在上面的字符串中,递归模式是“abc”,它在一个字符串中重复,因为这个字符串只递归地包含“abc”模式

如果我将此字符串作为参数传递给函数/方法,则函数/方法应返回“此字符串具有递归模式”。如果该字符串没有任何递归模式,则函数/方法应返回“此字符串不包含递归模式”。

以下是概率

String str1 = "abcabcabc"; 
//This string contains recursive pattern 'abc'

String str2 = "abcabcabcabdac"; 
//This string doesn't contains recursive pattern

String str2 = "abcddabcddabcddddabc";
//This string contains recursive pattern 'abc' & 'dd'
有人能给我建议解决方案/算法吗?我正在努力解决这个问题。对于不同的概率,我实现的最佳方法是什么

public boolean repeatedSubstringPattern(String str){
int l=str.length();
对于(int i=l/2;i>=1;i--){
如果(l%i==0){
int m=升/升;
String subS=str.substring(0,i);
StringBuilder sb=新的StringBuilder();
对于(int j=0;jFrom

public boolean repeatedSubstringPattern(String str){
int l=str.length();
对于(int i=l/2;i>=1;i--){
如果(l%i==0){
int m=升/升;
String subS=str.substring(0,i);
StringBuilder sb=新的StringBuilder();

for(int j=0;j解决方案不是用Javascript编写的。不过,这个问题看起来很有趣,所以尝试用
python
来解决它。抱歉

python
中,我编写了一个有效的逻辑[可以编写得更好,认为该逻辑会对您有所帮助]

剧本是

def check(lst):
    return all(x in lst[-1] for x in lst)

s = raw_input("Enter string:: ")
if check(sorted(s.split(s[0])[1:])):
    print("String, {} is recursive".format(s))
else:
    print("String, {} is NOT recursive".format(s))
脚本的输出:

[mac] kgowda@blr-mp6xx:~/Desktop/my_work/play$ python dup.py 
Enter string:: abcabcabcabdac
String, abcabcabcabdac is NOT recursive
[mac] kgowda@blr-mp6xx:~/Desktop/my_work/play$ python dup.py 
Enter string:: abcabcabc
String, abcabcabc is recursive
[mac] kgowda@blr-mp6xx:~/Desktop/my_work/play$ python dup.py 
Enter string:: abcddabcddabcddddabc
String, abcddabcddabcddddabc is recursive

解决方案不是用Javascript编写的。不过,这个问题看起来很有趣,所以尝试用
python
来解决。抱歉

python
中,我编写了一个有效的逻辑[可以编写得更好,认为该逻辑会对您有所帮助]

剧本是

def check(lst):
    return all(x in lst[-1] for x in lst)

s = raw_input("Enter string:: ")
if check(sorted(s.split(s[0])[1:])):
    print("String, {} is recursive".format(s))
else:
    print("String, {} is NOT recursive".format(s))
脚本的输出:

[mac] kgowda@blr-mp6xx:~/Desktop/my_work/play$ python dup.py 
Enter string:: abcabcabcabdac
String, abcabcabcabdac is NOT recursive
[mac] kgowda@blr-mp6xx:~/Desktop/my_work/play$ python dup.py 
Enter string:: abcabcabc
String, abcabcabc is recursive
[mac] kgowda@blr-mp6xx:~/Desktop/my_work/play$ python dup.py 
Enter string:: abcddabcddabcddddabc
String, abcddabcddabcddddabc is recursive

如果你事先知道“部分”,那么答案可能是,看起来

因此对于
abcabc
我们需要一个类似
abc(?R)*
的表达式,其中:

  • abc匹配文字字符
  • (?R)递归该模式
  • A*在零次和无限次之间匹配
第三个有点棘手。看,但它看起来像:

((abc)|(dd))(?R)*

其中我们有“abc”或“dd”,并且有任意数量的


否则,我看不出你怎么能仅仅从一个字符串判断它有这样一些未定义的递归结构。

如果你事先知道“部分”,那么答案可能是,看起来

因此对于
abcabc
我们需要一个类似
abc(?R)*
的表达式,其中:

  • abc匹配文字字符
  • (?R)递归该模式
  • A*在零次和无限次之间匹配
第三个有点棘手。看,但它看起来像:

((abc)|(dd))(?R)*

其中我们有“abc”或“dd”,并且有任意数量的


否则,我看不出如何仅从一个字符串判断它是否具有这样的未定义递归结构。

这也可以使用

我们的想法是建立一个一维数组,每个条目代表单词中的一个字符。对于单词中的每个字符
i
,我们检查单词up
0到i
中是否有前缀,该前缀也是后缀。原因是如果我们有公共后缀和前缀,我们可以在前缀结束后继续从字符中搜索使用相应的字符索引更新数组

对于
s=“abcab”
,数组将

Index : 0 1 2 3 4 5 6 7 8 
String: a b c a b c a b c 
KMP   : 0 0 0 1 2 3 4 5 6 
对于
Index=2
,我们发现在
Index=2

对于
Index=4
,后缀ab(Index=3,4)与前缀ab(Index=0,1)相同,因此我们更新
KMP[4]=2
,这是我们必须从中恢复搜索的模式的索引

因此
KMP[i]
保存字符串
s
的索引,其中前缀匹配范围
0到i
加1中可能最长的后缀。这本质上意味着前缀的长度
index+1-KMP[index]
以前存在于字符串中。使用此信息,我们可以找出该长度的所有子字符串是否相同

对于
Index=8
,我们知道
KMP[Index]=6
,这意味着有一个长度
9-6=3
的前缀(
s[3]到s[5]
),它等于后缀(
s[6]到s[8]
),如果这是我们唯一的重复模式,那么它将遵循

要更清楚地解释这个算法,请。 此表可以在线性时间内构建

vector<int> buildKMPtable(string word)
{
    vector<int> kmp(word.size());
    int j=0;
    for(int i=1; i < word.size(); ++i)
    {
        j = word[j] == word[i] ? j : kmp[j-1];
        if(word[j] == word[i])
        {
             kmp[i] = j + 1;
            ++j;
        }
        else
        {
            kmp[i] = j;
        }
    }
    return kmp;
}

bool repeatedSubstringPattern(string s) {
    auto kmp = buildKMPtable(s);    
    if(kmp[s.size() -1] == 0) // Occurs when the string has no prefix with suffix ending at the last character of the string
    {
        return false;
    }
    int diff = s.size() - kmp[s.size() -1]; //Length of the repetitive pattern
    if(s.size() % diff != 0) //Length of repetitive pattern must be a multiple of the size of the string
    {
        return false;
    }
// Check if that repetitive pattern is the only repetitive pattern. 
    string word = s.substr(0, diff);
    int w_size = word.size();
    for(int i=0; i < w_size; ++i)
    {
        int j = i;
        while(j < s.size())
        {
            if(word[i] == s[j])
            {
                j += w_size;    
            }
            else
            {
                return false;
            }
        }
    }
    return true;
}
vector buildKMPtable(字符串字)
{
向量kmp(word.size());
int j=0;
for(int i=1;i