酷炫ovo 2026-01-26 20:44 采纳率: 100%
浏览 13
已结题

洛谷p5734全WA(C语言)

为什么在VS2022里自测用例正确,到洛谷里就全WA
题目https://www.luogu.com.cn/problem/P5734

#include<stdio.h>
#include<string.h>
int main()
{
    int n = 0;
    char str[101] = { 0 };
    scanf("%d", &n);
    getchar();
    scanf("%s", str);
    getchar();
    char operator[100][108] = { 0 };
    for (int i = 0; i < n; i++)
    {
        fgets(operator[i], 108, stdin);
        operator[i][strcspn(operator[i], "\r")] = '\0';
        operator[i][strcspn(operator[i], "\n")] = '\0';
    }
    for (int i = 0; i < n; i++)
    {
        int len = strlen(str);
        int len1 = strlen(operator[i]);
        int count = 2;
        int j = 0, k = 0;
        switch (operator[i][0])
        {
        case '1':
            for (j = len, k = 2; j < 100 && k < len1; j++, k++)
            {
                str[j] = operator[i][k];
            }
            str[j] = '\0';
            printf("%s\n", str);
            break;
        case '2':
            while (operator[i][count]>='0'&&operator[i][count]<='9')
            {
                j = j * 10 + (operator[i][count] - '0');
                count++;
            }
            count++;
            while (operator[i][count] >= '0' && operator[i][count] <= '9')
            {
                k = k * 10 + (operator[i][count] - '0');
                count++;
            }
            for (int b = 0; j < 100 && b < k; j++, b++)
            {
                str[b] = str[j]; 
            }
            str[k] = '\0';
            printf("%s\n", str);
            break;
        case '3':
            while (operator[i][count] >= '0' && operator[i][count] <= '9')
            {
                j = j * 10 + (operator[i][count] - '0');
                count++;
            }
            count++;
            len1 -= count;
            for (; len >= j + 1; len--)
            {
                str[len + len1 - 1] = str[len - 1];
            }
            while (operator[i][count]!='\0')
            {
                str[j] = operator[i][count];
                count++;
                j++;
            }
            printf("%s\n", str);
            break;
        case '4':
            len1 -= count;
            if (len1 > len)
            {
                printf("-1\n");
                continue;
            }
            int find = 0;
            for (count = 0; count < len - len1 + 1; count++)
            {
                int mid = 1;
                for (j = count, k = 2; j < count + len1 && k < len1 + 2; j++, k++)
                {
                    if (str[j] != operator[i][k])
                    {
                        mid = 0;
                        break;
                    }
                }
                if (mid)
                {
                    printf("%d\n", count);
                    find = 1;
                    break;
                }
            }
            if (!find)
            {
                printf("-1\n");
            }
            break;
        }
    }
    return 0;
}


  • 写回答

4条回答 默认 最新

  • 酷炫ovo 2026-01-27 17:24
    关注

    问题已解决,感谢各位!

    #include<stdio.h>
    #define MAX_LEN 1000
    int my_strlen(char* ch)
    {
        int len = 0;
        while (ch[len] != '\0')
        {
            len++;
        }
        return len;
    }
    void my_strcat(char* doc, char* temp)
    {
        int doc_len = my_strlen(doc);
        int temp_len = my_strlen(temp);
        for (int i = 0; i < temp_len; i++)
        {
            doc[doc_len] = temp[i];
            doc_len++;
        }
        doc[doc_len] = '\0';
    }
    void my_substr(char* doc, int a, int b)
    {
        for (int i = 0; i < b; i++)
        {
            doc[i] = doc[a];
            a++;
        }
        doc[b] = '\0';
    }
    void my_insert(char* doc, int a, char* temp)
    {
        int doc_len = my_strlen(doc);
        int temp_len = my_strlen(temp);
        for (int i = doc_len-1; i >= a; i--)
        {
            doc[i + temp_len] = doc[i];    
        }
        for (int i = 0; i < temp_len; i++)
        {
            doc[a] = temp[i];
            a++;
        }
        doc[doc_len + temp_len] = '\0';
    }
    int my_strfind(char* doc, char* temp)
    {
        int doc_len = my_strlen(doc);
        int temp_len = my_strlen(temp);
        if (temp_len > doc_len)
        {
            return -1;
        }
        else
        {
            int find = 0;
            for (int i = 0; i < doc_len-temp_len+1; i++)
            {
                int mid = 1;
                if (doc[i] == temp[0])
                {
                    for (int j = 0,k=i; j < temp_len; j++,k++)
                    {
                        if (doc[k] != temp[j])
                        {
                            mid = 0;
                            break;
                        }
                    }
                    if (mid)
                    {
                        find = 1;
                        return i;
                    }
                }
            }
            if (!find)
                return -1;
        }
    }
    int main()
    {
        int q = 0, op = 0;
        char doc[MAX_LEN] = { 0 };
        char temp[MAX_LEN] = { 0 };
        scanf("%d", &q);
        getchar();
        scanf("%s", doc);
        for (int i = 0; i < q; i++)
        {
            getchar();
            scanf("%d", &op);
            switch (op)
            {
            case 1:
            {
                scanf("%s", temp);
                my_strcat(doc, temp);
                printf("%s\n", doc);
                break;
            }
            case 2:
            {
                int a, b;
                scanf("%d%d", &a, &b);
                my_substr(doc, a, b);
                printf("%s\n", doc);
                break;
            }
            case 3:
            {
                int a;
                scanf("%d %s", &a, temp);
                my_insert(doc, a, temp);
                printf("%s\n", doc);
                break;
            }
            case 4:
            {
                scanf("%s", temp);
                int a=my_strfind(doc, temp);
                printf("%d\n", a);
                break;
            }
            }
        }
        return 0;
    }
    
    

    我定义的数组过小,并且有细小的错误,改正后,我提高了代码的规范性,并定义MAX=1000,能涵盖大多数测试用例,改正了许多细小错误。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月27日
  • 已采纳回答 1月27日
  • 创建了问题 1月26日