1919, 11328, 18258

최명수·2021년 1월 18일

1919

1.아이디어
이중for문을 만들어서 두문자열을 돌면서 같은 문자를 찾으면 '0'둘다 바꿔준다 .그러면 a와b에는 서로 다른 문자만남았으니 for문 을써서 '0'이아닌문자가 다른문자 이니 count++;를 해준다

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;
int main()

{
    int count=0,max=0;
    string a,b;
    cin>>a;     
    cin>>b;
    sort(a.begin(),a.end());
    sort(b.begin(),b.end());
    int len=a.size();    
    int lenb=b.size();
    if(len>lenb)
    {
        max=len;
    }
    else
    {
        max=lenb;
    }
    
    //sring a와 b를 돌면서 같은 문자가있으면 둘다 문자0으로바꾸고 count++
    for(int i=0; i<len; i++)
    {
        for(int j=0; j<lenb; j++)
        {
            if(a[i]==b[j])
            {
                a[i]='0';
                b[j]='0';                
                break;
            }            
        }
    }
    //a와 b에 다른문자만 남음
    for(int i=0; i<len; i++)
    {
        if(a[i]!='0')
        {
            count++;
        }
    }
    for(int i=0; i<lenb; i++)
    {
        if(b[i]!='0')
        {
            count++;
        }
    }
    cout<<count;

    
}

3.깨달은점

11328번

1.아이디어
1919번과 비슷한 문제로서 두문자열을 비교하며 같은 문자를 찾으면 '0'으로바꿔준다. 그리고a와b의 '0'이 아닌 문자를 찾는 for문을 써서 acount++와bcount++해줄때 acount와alen이랑 같고 bcount랑blen이랑비교해서 Impossible을 출력하고 아니면 Possible를출력.

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
    int n,alen=0,blen=0,acount=0,bcount;
    string a,b;
    cin>>n;
    cin.ignore();
    

    for(int i=0; i<n; i++)
    {
        alen=0,blen=0,acount=0,bcount=0;
        cin>>a>>b;
        alen=a.length();
        blen=b.length();
        for(int j=0; j<alen; j++)
        {
            for(int k=0; k<blen; k++)
            {
                if(a[j]==b[k])
                {
                    a[j]='0';
                    b[k]='0';
                    break;
                }
            }
        }
        for(int j=0; j<alen; j++)
        {
            if(a[j]=='0')
            {
                acount++;
            }
        }
        for(int j=0; j<blen; j++)
        {
            if(b[j]=='0')
            {
                bcount++;
            }
        }
        if(acount==alen&&bcount==blen)
        {
            cout<<"Possible"<<'\n';

        }
        else
        {
            cout<<"Impossible"<<'\n';
        }      
        

    }
        

}

3.깨달은점

18258번

1.아이디어
처음에 vector를 이용해 코드를 구현하였으나 pop에서 구현이 더이상불가능함을 알고 구글링을하여 deque라는 새로운 container를 배움. 큐를 써도 된다.

#include <iostream>
#include <string>
#include <deque>

using namespace std;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(0);    
    int n;
    deque <int> dq;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        string str;
        cin.ignore();
        cin>>str;
        if(str=="push")
        {
            int tmp;
            cin>>tmp;
            dq.push_front(tmp);           

        }
        if(str=="front")
        {   
            if(dq.size()==0)
            {
                cout<<-1<<'\n';
            }
            else
            {
                cout<<dq.back()<<'\n';
            }                          
            
        }
        if(str=="back")
        {
            if(dq.size()==0)
            {
                cout<<-1<<'\n';
            }
            else
            {
                cout<<dq.front()<<'\n';
            }
        }
        if(str=="size")
        {
            cout<<dq.size()<<'\n';
        }
        if(str=="empty")
        {
            if(dq.size()==0)
            {
                cout<<1<<'\n';
            }
            else
            {
                cout<<0<<'\n';
            }
            
        }
        if(str=="pop")
        {
            if(dq.size()==0)
            {
                cout<<-1<<'\n';
            }
            else
            {
                cout<<dq.back()<<'\n';
                dq.pop_back();
            }
            
        }
        
    }
    return 0;
}

3.깨달은점
deque는 vector의 단점을 보안하기 위해서 만들어진 container이다. 그리고 deque는 데이터의 삽입과 삭제가 front와 back에서 이루어진다. 선언은 deque<date type> 변수이름 이다. 전반적인 멤버함수들은 벡터와 유사하지만 데이터의 삽입이 다르다는것만 기억하자!

0개의 댓글