AcWing 776. 字符串移位包含问题 (困难级) 我与chatgpt共同找问题版

对于一个字符串来说,定义一次循环移位操作为:将字符串的第一个字符移动到末尾形成新的字符串。

给定两个字符串 s1和 s2,要求判定其中一个字符串是否是另一字符串通过若干次循环移位后的新字符串的子串。

例如 CDAA 是由 AABCD 两次移位后产生的新串 BCDAA 的子串,而 ABCD 与 ACBD 则不能通过多次移位来得到其中一个字符串是新串的子串。

输入格式

共一行,包含两个字符串,中间由单个空格隔开。

字符串只包含字母和数字,长度不超过 3030。

输出格式

如果一个字符串是另一字符串通过若干次循环移位产生的新串的子串,则输出 true,否则输出 false

输入样例:
AABCD CDAA
输出样例:
true
难度:困难
时/空限制:1s / 64MB
总通过数:15287
总尝试数:38418
来源:语法题
算法标签

在每一次移位以后判断是否有子序列
在这个程序里默认第一个字符串的长度大于第二个字符串 因此小于的情况就要交换 之前只交换了字符串没有交换x和y
如果直接写.size()就可以避免这个问题了 为了省事省出麻烦来了 //angry

以下是我和chatgpt的对话  图一乐 它第一次也没有看出来

我:

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
    string a,b;
    cin>>a>>b;

    int x=a.size();
    int y=b.size();
    int t=0;
    if(x<y)  
    {
        swap(a,b);
    }
       // cout<<a<<endl<<b<<endl;
  
    for(int i=0;i<x;i++)
    {
        a=a+a[0];
        a=a.substr(1,x);
        for(int j=0;j<x;j++)
        if(b==a.substr(j,y))
        {
            cout<<"true"<<endl;
            return 0;
        }
        
    }
   
    cout<<"false"<<endl;
    return 0;
}

为什么我明明写了当b的长度大于a的时候的交换程序,当b的长度大于a时输出的结果还是不对呢

它:您的代码在将字符串交换时,只进行了一次交换。如果在交换后,条件仍然满足,即b的长度仍然大于a,那么交换后的字符串仍然不符合预期。

为了解决这个问题,您可以使用一个while循环,直到a的长度超过b时停止交换。修改后的代码如下:

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int main()
{
    string a, b, c;
    cin >> a >> b;

    int x = a.size();
    int y = b.size();

    if (x < y)
    {
        // 交换a和b,使得a的长度大于等于b
        swap(a, b);
    }

    cout << a << endl << b << endl;
  
    while (a.size() >= b.size())
    {
        for (int i = 0; i < x; i++)
        {
            a = a + a[0];
            a = a.substr(1, x);
            
            for (int j = 0; j < x; j++)
            {
                if (b == a.substr(j, y))
                {
                    cout << "true" << endl;
                    return 0;
                }
            }
        }
        
        if (a.size() >= b.size())
        {
            // 交换a和b,继续循环交换
            swap(a, b);
        }
    }
   
    cout << "false" << endl;
    return 0;
}
 

我:你这是死循环 会超时的

它:

非常抱歉,我犯了一个错误。我明白您不想要一个无限循环。我的解决方案确实是错误的。

这里有另一种方法可以解决这个问题。我们可以找到一个字符串c,使得c是a的多个重复字符串的连接,并检查b是否是c的子串。这样可以避免无限循环的问题。

下面是修改后的代码:

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

bool isRotation(string a, string b)
{
    if(a.length() != b.length()) {
        return false;
    }
    
    string c = a + a;
    return c.find(b) != string::npos;
}

int main()
{
    string a, b;
    cin >> a >> b;

    if(isRotation(a, b)) {
        cout << "true" << endl;
    }
    else {
        cout << "false" << endl;
    }
  
    return 0;
}

isRotation函数中,我们首先检查两个字符串的长度是否相等。如果长度不同,那么b不可能是a的循环旋转。然后,我们将a重复一次,将其与自身连接,形成字符串c。最后,我们使用find()函数来查找b是否是c的子串。如果是,那么说明b是a的循环旋转,返回true,否则返回false。

希望这次的解决方案符合您的期望,非常抱歉之前给您带来了困扰。

我:不,你不明白我的问题 当我输入的第一个字符串长度大于第二个字符串时 可以正常运行 因此我加了判断条件 当第一个字符串长度小于第二个字符串时我就让他们交换 按照道理交换以后就满足第一个字符串长度大于第二个字符串了,可以正常运行,然而运行的结果是错误的

它:非常抱歉之前我理解错了您的问题。现在我明白了您的意图,您希望检查两个字符串是否满足循环旋转的条件。对于输入的第一个字符串长度小于第二个字符串的情况,您希望通过交换字符串来使得第一个字符串的长度大于第二个字符串。

我看到您尝试在主函数中进行了交换操作,但是可能存在一些问题。请尝试以下修改您的代码:

#include <iostream>
#include <algorithm>
using namespace std;

bool isRotation(string a, string b)
{
    int x = a.size();
    int y = b.size();
    if (x < y)
    {
        swap(a, b);
        swap(x, y);
    }

    for (int i = 0; i < x; i++)
    {
        a = a + a[0];
        a = a.substr(1, x);
        for (int j = 0; j < x; j++)
        {
            if (b == a.substr(j, y))
            {
                return true;
            }
        }
    }

    return false;
}

int main()
{
    string a, b;
    cin >> a >> b;

    if (isRotation(a, b))
    {
        cout << "true" << endl;
    }
    else
    {
        cout << "false" << endl;
    }

    return 0;
}
 

问题至此解决 以下是最后的代码(虽然也很暴力)

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
    string a,b;
    cin>>a>>b;

    int x=a.size();
    int y=b.size();
    int t=0;
    if(x<y)  
    {
        swap(a,b);
        swap(x,y);   //chatgpt提醒后发现的问题
    }
       // cout<<a<<endl<<b<<endl;

    for(int i=0;i<x;i++)
    {
        a=a+a[0];
        a=a.substr(1,x);
        for(int j=0;j<x;j++)   //判断是否有子序列的程序
        if(b==a.substr(j,y))
        {
            cout<<"true"<<endl;
            return 0;
        }

    }

    cout<<"false"<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值