0..1수열 시간초과 도와주세요.ㅜㅜㅜㅜㅜㅜ

  • zephyr
    zephyr
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main(void)
    {
        char* arr = new char[sizeof(arr)];
        int number;
        int first, second;
        bool test;
    
        cin>>arr;
        cin>>number;
    
        for(int testcase=0; testcase<number; testcase++)
        {
            cin>>first;
            cin>>second;
    
            if(second<first)
            {
                int temp;
                temp = first;
                first = second;
                second = temp;
            }
    
            for(int i=first; i<second; i++)
            {
                if(arr[i] != arr[i+1])
                {
                    test = false;
                    //cout<<"No"<<endl;
                    //goto des;
                }
            }
    
            if(test == false)
                cout<<"No"<<endl;
            else
                cout<<"Yes"<<endl;
    
            test = true;
                //des:;
    
        }
    }
    
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main(void)
    {
        string arr;
        int number;
        int first, second;
        bool test;
    
        cin>>arr;
        cin>>number;
    
        for(int testcase=0; testcase<number; testcase++)
        {
            cin>>first;
            cin>>second;
    
            if(second<first)
            {
                int temp;
                temp = first;
                first = second;
                second = temp;
            }
    
            for(int i=first; i<second; i++)
            {
                if(arr[i] != arr[i+1])
                {
                    test = false;
                    //cout<<"No"<<endl;
                    //goto des;
                }
            }
    
            if(test == false)
                cout<<"No"<<endl;
            else
                cout<<"Yes"<<endl;
    
            test = true;
                //des:;
    
        }
    }
    

    여러 방법 써봤는데 시간 초과 나오네요.ㅜㅜ

    어느부분이 문제인지좀 알려주세여


    11년 전
2개의 댓글이 있습니다.
  • JongMan
    JongMan

    너무 느린 것이 문제입니다 ㅋㅋㅋ
    first second 입력받았을 때 for 문을 돌지 않고 문제를 풀수 있나 고민해보세요


    11년 전 link
  • includex
    includex

    저랑 같은 고민을 하셨군요..ㅠㅠ 저도 한참 고민하다 스스로 풀지는 못했습니다. 이런 케이스를 처리하는 방법에 대해 배웠습니다. 문제의 요점은 "수열입력을 한번만 받아 그 수열을 처리하는 문제" 입니다. 그래서 속도를 줄이는 부분도....


    11년 전 link
  • 정회원 권한이 있어야 커멘트를 다실 수 있습니다. 정회원이 되시려면 온라인 저지에서 5문제 이상을 푸시고, 가입 후 7일 이상이 지나셔야 합니다. 현재 문제를 푸셨습니다.