백준문제풀이
백준 문제 24510번 시간복잡도를 배운 도도 문제풀이 c++
노가다 김씨
2022. 3. 6. 21:34
https://www.acmicpc.net/problem/24510
24510번: 시간복잡도를 배운 도도
도도는 이번 신촌캠프에서 시간복잡도의 개념에 대해 배웠다. 하지만 듣다가 졸려서 자버린 결과 오개념을 가져 버렸는데, 바로 반복문의 개수로만 시간복잡도를 판단한다는 것이다. 시간복
www.acmicpc.net
문제해석
주어진 문자열 C개중에서 while개수 + for 개수 가 가장 많은 것을 답으로 출력하면된다.
코드
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int cnt=0;
int ans=0;
int main(){
cout.tie(NULL);
cin.tie(NULL);
ios_base::sync_with_stdio(false);
int c;
cin>>c;
string s;
for(int i=0; i<c; i++)
{
cin>>s;
if(s.size()>=3)
{
for(int j=0; j<s.size()-2;)
{
if(s[j]=='f'&&s[j+1]=='o'&&s[j+2]=='r')
{
cnt++;
j=j+3;
}
else
{
j++;
}
}
}
if(s.size()>=5)
{
for(int j=0; j<s.size()-4;)
{
if(s[j]=='w'&&s[j+1]=='h'&&s[j+2]=='i'&&s[j+3]=='l'&&s[j+4]=='e')
{
cnt++;
j=j+5;
}
else
{
j++;
}
}
}
ans=max(ans,cnt);
cnt=0;
}
cout<<ans;
}
사실 파이썬으로 작성하면 엄청 짧게 짤수가있었다.
파이썬 코드
import re
ans=0
cnt=0
c=int(input())
for i in range(0,c):
text=input()
cnt=text.count('for')+text.count('while')
if cnt>ans:
ans=cnt
cnt=0
print(ans)