如何在字符串中查找字符串

时间:2010-03-29 12:31:44

标签: c++ string

我不知何故需要在其他字符串中找到最长的字符串,所以如果string1为“Alibaba”而string2为“ba”,则最长的字符串为“baba”。我有字符串的长度,但下一步是什么?

char* fun(char* a, char& b) 
{
int length1=0;
int length2=0;
int longer;
int shorter;
char end='\0';

while(a[i] != tmp)
{
    i++;
    length1++;
}

int i=0;
while(b[i] != tmp)
{
    i++;
    length++;
}

if(dlug1 > dlug2){
    longer = length1;
    shorter = length2;
}
else{
    longer = length2;
    shorter = length1;
}

 //logics here
   }

int main()
{
char name1[] = "Alibaba";
char name2[] = "ba";
char &oname = *name2;

cout << fun(name1, oname) << endl;

system("PAUSE");
return 0;
}

5 个答案:

答案 0 :(得分:2)

哇这个问题的答案很多。以下是您的代码应该执行的操作:

  1. 使用标准字符串搜索功能查找“ba”的第一个实例。
  2. 在循环中查看此“ba”以查看接下来N个字符中有多少也是“ba”。
  3. 如果此序列长于先前记录的最长序列,请保存其长度和位置。
  4. 在最后一个之后找到下一个“ba”的实例。
  5. 这是代码(未经测试):

    string FindLongestRepeatedSubstring(string longString, string shortString)
    {
        // The number of repetitions in our longest string.
        int maxRepetitions = 0;
    
        int n = shortString.length(); // For brevity.
    
        // Where we are currently looking.
        int pos = 0;
        while ((pos = longString.find(shortString, pos)) != string::npos)
        {
            // Ok we found the start of a repeated substring. See how many repetitions there are.
            int repetitions = 1;
            // This is a little bit complicated.
            // First go past the "ba" we have already found (pos += n)
            // Then see if there is still enough space in the string for there to be another "ba"
            // Finally see if it *is* "ba"
            for (pos += n; pos+n < longString.length() && longString.substr(pos, n) == shortString; pos += n)
                ++repetitions;
            // See if this sequence is longer than our previous best.
            if (repetitions > maxRepetitions)
                maxRepetitions = repetitions;
        }
        // Construct the string to return. You really probably want to return its position, or maybe
        // just maxRepetitions.
        string ret;
        while (maxRepetitions--)
            ret += shortString;
        return ret;
    }
    

答案 1 :(得分:0)

http://www.cplusplus.com/reference/string/string/find/

也许你是故意这样做的,但是你应该使用std :: string类并忘记像char * string表示这样的古老的东西。 它将使您能够使用许多优化方法,如字符串研究等。

答案 2 :(得分:0)

你想要的应该看起来像这个伪代码:

i = j = count = max = 0
while (i < length1 && c = name1[i++]) do
  if (j < length2 && name2[j] == c) then
    j++
  else
    max = (count > max) ? count : max
    count = 0
    j = 0
  end
  if (j == length2) then
    count++
    j = 0
  end
done
max = (count > max) ? count : max
for (i = 0 to max-1 do
  print name2
done

这个想法在这里,但我觉得可能存在一些这种算法不起作用的情况(具有复杂重叠的情况需要返回name1)。你可能想看一下Boyer-Moore算法并将两者混合起来得到你想要的东西。

答案 3 :(得分:0)

Algorithms Implementation Wikibook可以在C ++中实现您想要的功能。

答案 4 :(得分:0)

为什么不使用C提供的strstr函数。

const char * strstr ( const char * str1, const char * str2 );
      char * strstr (       char * str1, const char * str2 );
Locate substring

Returns a pointer to the first occurrence of str2 in str1,
or a null pointer if str2 is not part of str1.
The matching process does not include the terminating null-characters.

现在使用长度并创建一个循环并使用原始字符串播放anf找到里面最长的字符串。