花辞树dor 2023-01-18 00:40 采纳率: 100%
浏览 29
已结题

【GPLT一阶】L1-064 估值一亿的AI核心代码 测试点2 答案错误

题目来源

img

地址:https://pintia.cn/problem-sets/994805046380707840/exam/problems/1111914599412858885
错误信息

img

代码内容

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int isBDFH(char ch) //标点符号或空格判断函数
{
    if ((ch != ' ' && ch != '\0' && !(ch >= '0' && ch <= '9') && !(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z')) || (ch == ' '))
        return 1;
    return 0;
}

int kongge(char str[]) //字符串空格处理函数
{
    int len = strlen(str);
    for (int i = 0; i < len; i++) //处理字符串开头空格
    {
        if (str[i] == ' ')
            str[i] = '\0';
        else
            break;
    }
    len = qingli(str, len);
    for (int i = len - 1; i >= 0; i--) //处理字符串末尾空格
    {
        if (str[i] == ' ')
            str[i] = '\0';
        else
            break;
    }
    len = qingli(str, len);
    for (int i = 0; i < len; i++) //处理字符串标点前空格
    {
        if (isBDFH(str[i]))
        {
            if (i - 1 >= 0 && str[i - 1] == ' ') //i-1<0则下标不存在,&&判断结束,后者数组不会越界访问
            {
                for (int j = i - 1; j >= 0; j--)
                {
                    if (str[j] == ' ')
                        str[j] = '\0';
                    else
                        break;
                }
            }
        }
    }
    len = qingli(str, len);
    for (int i = 0; i < len; i++) //处理字符串中间的多个空格
    {
        if (str[i] != ' ')
        {
            if (i + 1 <= len - 1 && str[i + 1] == ' ') //找到一个非空格字符,它的下一个字符是空格
            {
                int count = 0;
                int j = i + 1;
                for (; j < len; j++)
                {
                    if (str[j] == ' ')
                        count++;
                    else
                        break;
                }
                for (int k = 1; k < count; k++) //删去其它空格,保留一个空格
                    str[i + k] = '\0';
                i = j - 1; //更新i
            }
        }
    }
    len = qingli(str, len);
    return len;
}

int qingli(char str[], int len) //字符串空字符清理函数
{
    char dest[10000] = { '\0' };
    int count = 0;
    for (int i = 0; i < len; i++)
    {
        if (str[i] != '\0')
        {
            dest[count++] = str[i];
        }
    }
    strcpy(str, dest);
    len = count;
    return len;
}

int duli(char str[]) //字符串的独立子字符串删除函数
{
    int len = strlen(str);
    int res = 0;
    char sta1[100] = { "can you" };
    char trans1[100] = { "A can" };
    for (int i = 0; i <= len - 1; i++)
    {
        if (str[i] == 'c' && len - i >= 7)
        {
            res = 1;
            for (int j = 1; j <= 6 && res == 1; j++)
            {
                if (str[i + j] != sta1[j])
                    res = 0;
            }
            if (res == 1) //匹配到目标字符串
            {
                if (i - 1 < 0 && i + 7 > len - 1) //前,后均是边界
                {
                    strcpy(str, "A can");
                }
                else if (i - 1 < 0 && isBDFH(str[i + 7])) //前是边界,后满足条件
                {
                    for (int j = 0; j <= 6; j++)
                        str[i + j] = trans1[j];
                }
                else if (i + 7 > len - 1 && isBDFH(str[i - 1])) //后是边界,前满足条件
                {
                    for (int j = 0; j <= 6; j++)
                        str[i + j] = trans1[j];
                }
                else if ((i - 1 >= 0 && i + 7 <= len - 1) && (isBDFH(str[i - 1]) && isBDFH(str[i + 7]))) //前后均非边界且均满足条件
                {
                    for (int j = 0; j <= 6; j++)
                        str[i + j] = trans1[j];
                }
                i = i + 6;//更新i,之后会i++
            }
        }
    }
    len = qingli(str, len);
    strcpy(sta1, "could you");
    strcpy(trans1, "A could");
    for (int i = 0; i <= len - 1; i++)
    {
        if (str[i] == 'c' && len - i >= 9)
        {
            res = 1;
            for (int j = 1; j <= 8 && res == 1; j++)
            {
                if (str[i + j] != sta1[j])
                    res = 0;
            }
            if (res == 1) //匹配到目标字符串
            {
                if (i - 1 < 0 && i + 9 > len - 1) //前,后均是边界
                {
                    strcpy(str, "A could");
                }
                else if (i - 1 < 0 && isBDFH(str[i + 9])) //前是边界,后满足条件
                {
                    for (int j = 0; j <= 8; j++)
                        str[i + j] = trans1[j];
                }
                else if (i + 9 > len - 1 && isBDFH(str[i - 1])) //后是边界,前满足条件
                {
                    for (int j = 0; j <= 8; j++)
                        str[i + j] = trans1[j];
                }
                else if ((i - 1 >= 0 && i + 9 <= len - 1) && (isBDFH(str[i - 1]) && isBDFH(str[i + 9]))) //前后均非边界且均满足条件
                {
                    for (int j = 0; j <= 8; j++)
                        str[i + j] = trans1[j];
                }
                i = i + 8;//更新i,之后会i++
            }
        }
    }
    len = qingli(str, len);
    strcpy(sta1, "me");
    strcpy(trans1, "you");
    for (int i = 0; i <= len - 1; i++)
    {
        if (str[i] == 'm' && len - i >= 2)
        {
            res = 1;
            for (int j = 1; j <= 1 && res == 1; j++)
            {
                if (str[i + j] != sta1[j])
                    res = 0;
            }
            if (res == 1) //匹配到目标字符串
            {
                if (i - 1 < 0 && i + 2 > len - 1) //前,后均是边界
                {
                    strcpy(str, "you");
                }
                else if (i - 1 < 0 && isBDFH(str[i + 2])) //前是边界,后满足条件
                {
                    for (int j = len - 1; j >= i + 2; j--) //me后面的字符后移1格
                        str[j + 1] = str[j];
                    for (int j = i, k = 0; j <= i + 2; j++, k++) //对应位置填充字符
                        str[j] = trans1[k];
                }
                else if (i + 2 > len - 1 && isBDFH(str[i - 1])) //后是边界,前满足条件
                {
                    for (int j = len - 1; j >= i + 2; j--)
                        str[j + 1] = str[j];
                    for (int j = i, k = 0; j <= i + 2; j++, k++)
                        str[j] = trans1[k];
                }
                else if ((i - 1 >= 0 && i + 2 <= len - 1) && (isBDFH(str[i - 1]) && isBDFH(str[i + 2]))) //前后均非边界且均满足条件
                {
                    for (int j = len - 1; j >= i + 2; j--)
                        str[j + 1] = str[j];
                    for (int j = i, k = 0; j <= i + 2; j++, k++)
                        str[j] = trans1[k];
                }
                i = i + 2;
                len += 1;
            }
        }
    }
    len = qingli(str, len);
    strcpy(sta1, "I");
    strcpy(trans1, "you");
    for (int i = 0; i <= len - 1; i++)
    {
        if (str[i] == 'I' && len - i >= 1)
        {
            res = 1;
            if (res == 1) //匹配到目标字符
            {
                if (i - 1 < 0 && i + 1 > len - 1) //前,后均是边界
                {
                    strcpy(str, "you");
                }
                else if (i - 1 < 0 && isBDFH(str[i + 1])) //前是边界,后满足条件
                {
                    for (int j = len - 1; j >= i + 1; j--) //I后面的字符后移2格
                        str[j + 2] = str[j];
                    for (int j = i, k = 0; j <= i + 2; j++, k++) //对应位置填充字符
                        str[j] = trans1[k];
                }
                else if (i + 1 > len - 1 && isBDFH(str[i - 1])) //后是边界,前满足条件
                {
                    for (int j = len - 1; j >= i + 1; j--)
                        str[j + 2] = str[j];
                    for (int j = i, k = 0; j <= i + 2; j++, k++)
                        str[j] = trans1[k];
                }
                else if ((i - 1 >= 0 && i + 1 <= len - 1) && (isBDFH(str[i - 1]) && isBDFH(str[i + 1]))) //前后均非边界且均满足条件
                {
                    for (int j = len - 1; j >= i + 1; j--)
                        str[j + 2] = str[j];
                    for (int j = i, k = 0; j <= i + 2; j++, k++)
                        str[j] = trans1[k];
                }
                i = i + 2;
                len += 2;
            }
        }
    }
    len = qingli(str, len);
    return len;
}

int main()
{
    int n;
    scanf("%d", &n);
    getchar();
    char str[10000] = { '\0' };
    for (int i = 0; i < n; i++)
    {
        gets(str);
        puts(str);
        int len = strlen(str);
        for (int j = 0; j < len; j++)
        {
            if (str[j] >= 'A' && str[j] <= 'Z' && str[j] != 'I')
                str[j] += 32;
            else if (str[j] == '?')
                str[j] = '!';
        }
        kongge(str);
        len = duli(str);
        for (int j = 0; j < len; j++)
        {
            if (str[j] == 'A')
                str[j] = 'I';
        }
        printf("AI: %s\n", str);
    }
    return 0;
}

备注
题目细节很多,代码写了很长,想不清楚是哪个地方没有考虑到。求解答,万分感谢!

  • 写回答

1条回答 默认 最新

  • 花辞树dor 2023-01-18 01:42
    关注

    问题解决,已结题,感谢浏览。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)