c++输入时为什么会有重复读取的情况,或者说不读取下一行的情况

  • 这是第二次读取,temp_i变量又读到4 b的字符4
    !

  • 输出又为-1
    !

  • 这是第三次读取,还是读取4 b这一行。
    !

  • 就。。。。很迷???

  • 题目连接,测试点是题目的第二个测试点

  • 输入为
    6
    aaaaaaaaaaaaaaaaaaaaaaa
    4 aaaaaaaa
    1 aaaaa
    4 b
    2 2 3
    3 0 fafafafa
    4 fafa

  • 输出为
    0
    aaaaaaaaaaaaaaaaaaaaaaaaaaaa
    -1
    aaa
    fafafafaaaa
    0

  • 下面是代码,注意如果再洛谷测试,需要把\n改为\r,可能他们直接将window的文件复制过去了吧

#include<iostream>
#include<cstring>
using namespace std;
const int STRLEN_MAX=1e6;
char temp_buf[STRLEN_MAX];
int temp_buf_count;
char buf[STRLEN_MAX];
int buf_count;
int main()
{
    int n;
    char temp_i;
    char temp_c;
    buf_count=0;
    cin>>n;
    cin>>buf[0];
    cin.get(buf+1,STRLEN_MAX,'\n');
    buf_count=strlen(buf);
    while(n--)
    {
        cin>>temp_i;
        switch(temp_i-'0')
        {
            case 1:
            cin>>buf[buf_count++];
            cin.get(buf+buf_count,STRLEN_MAX,'\n');
            buf_count=strlen(buf);
            cout<<buf<<endl;
            break;

            case 2:
            int index,num;
            buf_count=0;
            cin>>index>>num;
            for(int i=index;i<index+num;i++)
                buf[buf_count++]=buf[i];
            buf[buf_count]='\0';
            cout<<buf<<endl;
            break;

            case 3:
            int temp_i_0;
            cin>>temp_i_0;
            cin>>temp_buf[0];
            cin.get(temp_buf+1,STRLEN_MAX,'\n');
            temp_buf_count=strlen(temp_buf);
            for(int i=0;i<temp_buf_count;i++)
            {
                for(int j=buf_count;j>temp_i_0+i;j--)
                {
                    buf[j]=buf[j-1];
                }
                buf_count++;
                buf[temp_i_0+i]=temp_buf[i];
            }
            buf[buf_count]='\0';
            cout<<buf<<endl;
            break;

            case 4:
            cin>>temp_buf[0];
            cin.get(temp_buf+1,STRLEN_MAX,'\n');
            temp_buf_count=strlen(temp_buf);
            char* temp_p;
            temp_p=strstr(buf,temp_buf);
            if(temp_p!=NULL)
                cout<<temp_p-buf<<endl;
            else
                cout<<-1<<endl;
            break;
        }
    }
    return 0;
}