求助,全WA,但下载测试样例本地调试后输出一样

P5734 【深基6.例6】文字处理软件

sanjuu2024 @ 2024-10-02 16:49:37

#include <iostream>
#include <string>
using namespace std;
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int q;
    cin >> q;
    string s;
    // 读取换行符
    cin.get();
    getline(cin,s);
    for (int i = 0; i < q; i++)
    {
        int n;
        cin >> n;
        string s2;
        switch(n)
        {
            case 1:
                {
                    cin >> s2;
                    s+=s2;
                    cout << s << endl;
                    break;
                }
            case 2:
                {
                    int n1,n2;
                    cin >> n1 >> n2;
                    s2="";
                    for (int i = n1; i < n1+n2; i++)
                    {
                        s2+=s[i];
                    }
                    s = s2;
                    cout << s << endl;
                    break;
                }
            case 3:
                {
                    int a;
                    cin >> a;
                    cin >> s2;
                    s.insert(a,s2);
                    cout << s << endl;
                    break;
                }
            case 4:
                {
                    cin >> s2;
                    if (s.find(s2) != string::npos)
                        cout << s.find(s2) << endl;
                    else
                        cout << -1 << endl;
                    break;
                }
        }
    }
    return 0;
}

by sanjuu2024 @ 2024-10-02 21:02:48

结帖,在评论区找到原因了。 先是把一句cin.get()增加到了两句,过了#4#5。 然后干脆把两句cin.get()和那句getline(cin,s)换成了cin >> s,直接AC了......(洛谷的输入真的有点诡异,印象中有些别的题的字符输入也不全是\r\n结尾的,似乎有用一句cin.get()也可以的?)


by cccckick @ 2024-10-22 22:06:31

@sanjuu2024 求大佬看下我的代码,我的cin换成getline之后就错了。


#include<bits/stdc++.h>
using namespace std;
int main()
{string str;
int n;
cin>>n;
cin.ignore();
getline(cin,str);
for(int i=0;i<n;i++)
{
    int x;
    cin>>x;
    if(x==1)
    {
        string temp;
        cin>>temp;
        str+=temp;
        cout<<str;
        cout<<endl;
    } 
    else if(x==2)
    {
        int x1,y1;
        cin>>x1>>y1;
        str=str.substr(x1,y1);
        cout<<str;
        cout<<endl;
    }
    else if(x==3)
    {
        int x1;
        cin>>x1;
        string temp;
        cin>>temp;
    str=str.insert(x1,temp);
        cout<<str;
        cout<<endl;

    }
    else {
    string temp;
    cin >> temp;

    size_t pos = str.find(temp);  
    if (pos != string::npos) {
        cout << pos << endl;  
    } else {
        cout << -1 << endl;  
    }
}

    }

}

by sanjuu2024 @ 2024-10-23 17:02:50

@cccckick 嗯...这道题用cin>>就可以过了虽然。我也不是很清楚,但之前拿别的题的代码问过一个大佬,说是建议少用getline,有时候就是会有奇妙的bugಠ_ಠ


|