0

我正在开发一个C程序,程序语句如下:

给定一个字符串 S 和一个正长度的字符串列表 F1,R1,F2,R2,...,FN,RN,继续按顺序查找 Fi 在 S 中的出现(从左到右)并替换他们和里。所有字符串都超过字母 { 0, 1 }。搜索应该只考虑在之前的迭代中没有被替换的 S 的连续片段。算法的迭代不应覆盖该算法的任何先前替换。

输入样本:

你的程序应该接受一个文件名的路径作为它的第一个参数。该文件中的每一行都是一个测试用例。每个测试用例将包含一个字符串,然后是一个分号,然后是一个逗号分隔的字符串列表。例如。

10011011001;0110,1001,1001,0,10,11

输出样本:

对于每一行输入,在替换完成后打印出字符串。例如。

11100110

例如:10011011001 => 10100111001 [将 0110 替换为 1001] => 10100110 [将 1001 替换为 0] => 11100110 [将 10 替换为 11] => 11100110

我的代码能够用 Rn 替换每个相应的 Fn,但它已经完成了之前的替换。如何只搜索那些没有被替换的 S 的连续片段。我正在按如下方式进行搜索和替换:

  void string_sub(char s1[],char s2[],char s3[])
    {
            char r[200];
            printf("\n%s %s %s\n",s1,s2,s3);
            int i,j,k,x,y;
            char res[100];
            int len1=strlen(s1);
            int len2=strlen(s2);
            int len3=strlen(s3);
            j=0;
            i=0;
            while(i<len1)//searching for string 2 in string 1
            {
                    if(s2[j]==s1[i])
                    {
                            j++;
                            i++;
                            if(j==len2)
                            {
                              y=i;
                              x=i-len2;
                              break;        
                            }
                    }
                    else
                    {
                            if(s2[0]!=s1[i])
                            i++;
                            j=0;
                    }
            }
            k=0;
            for(i=0;i<x;i++)
            {
                    r[k]=s1[i];
                    k++;
            }
            for(i=0;i<len3;i++)
            {
                r[k]=s3[i];
                    k++;
            }
            i=y;
            while(i<len1)
            {
                    r[k]=s1[i];
                    k++;
                    i++;
            }
            r[k]='\0';//resultant string into r
            k=0;
           for(i=0;i<strlen(r);i++)//copying resultant string to s1
           {
            s1[k]=r[i];
            k++;
           }
           s1[k]='\0';
    }

我的整个 C 代码在这里

4

1 回答 1

2

It seems to me, if I understand your code correctly, that you are calling your substitution code for each pair you get, and passing in the converted string each time. What I would do is pass a start offset into that function as well and then have that function return the location in the newly substituted string that you left off with the last substitution. The function would start searching s1 at your offset rather than at 0 each time. (Remember to not change your offset if you do not perform a substitution.) So in you main function, have an offset variable that starts at 0 and pass that in to the function, and also assign the result of the function to that variable so that next time you call it you will start at the last offset instead of the beginning. Does this make sense? If not I can try to break my answer down a bit better.

In your main function you would:

int offset = 0;

and then

offset = string_sub( offset, s1, s2, s3 );

your function will look something like this:

int string_sub( int offset, char* s1, char* s2, char* s3 )
{
...

and at the point where you have finished replacing the string you would:

offset = y;

and at the end you would do this

return( offset );
}

Does that help?

As @Mike noticed, my answer is not quite correct. I assumed incorrectly that you would ignore everything before the last point you looked at because I did not read the requirements thoroughly enough. Instead of passing in an offset and then returning a new offset, you need to pass in a structure that contains an array of ranges where each range is a range of characters that have been substituted and are as such, untouchable, and a count of how many ranges there are in that array. Since this is in C we don't have the luxury of easy array management so we will have to do this the hard way. I would make the structure look as follows:

#define MAX_RANGES (10)

struct RangeEntry
{
    int first;
    int last;
};

struct RangeData
{
    int numRanges;
    struct RangeEntry ranges[MAX_RANGES];
};

And then have your substitute function prototype look like this:

int string_sub( char* s1, char* s2, char* s3, struct RangeData* rangeData )

In your main function you would instantiate this structure, and initialize numRanges to 0, and then pass a pointer to this instance into your function. In your function you would have to look at s1 a little differently. Instead of just looking to see if you ran off the end of the string you would have to look to see if you run into any of those ranges. If you are matching then the match will have to end at the start of the next range. If you are not then you would have to continue looking after the end of the next range.

I hope this helps, and sorry for the wrong answer earlier.

于 2013-03-16T13:23:38.500 回答