728x90
programmers.co.kr/learn/courses/30/lessons/1830
#include <string>
#include <iostream>
#include <map>
using namespace std;
// 전역 변수를 정의할 경우 함수 내에 초기화 코드를 꼭 작성해주세요.
string solution(string sentence) {
string answer = "";
int expect=0; //0,1,2,3,4
map<char,bool>m;
int t=0;
while(sentence.size()>0){
if(sentence.size()>=3){
if(isupper(sentence[0])&&isupper(sentence[1])){expect=0;}
else if(isupper(sentence[0])&&islower(sentence[1])){expect=1;}
else if(islower(sentence[0])&&isupper(sentence[1])&&isupper(sentence[2])){expect=2;}
else if(islower(sentence[0])&&isupper(sentence[1])&&islower(sentence[2])&&(sentence[0]!=sentence[2])){expect=3;}
else{return "invalid";}
}
else{
for(int l=0; l<sentence.size(); l++){
if(islower(sentence[l])){return "invalid";}
}
return sentence;
}
string str;
if(expect ==0){
int i =0;
while(isupper(sentence[i])&& i<sentence.size()){
i++;
}
str = sentence.substr(0,i);
}
else if(expect ==1)
{
char origin = sentence[1];
if(m[origin]==true){ return "invalid";}
else{m[origin]=true;}
int i=1;
while(i<sentence.size()){
if(sentence[i]!=origin){
break;
}
sentence.erase(i,1);
if(islower(sentence[i])){return "invalid";}
i++;
}
str = sentence.substr(0,i);
}
else if(expect==2){
int i=1;
char origin = sentence[0];
if(m[origin]==true){ return "invalid";}
else{m[origin]=true;}
bool toggle = false;
while(i<sentence.size()){
if(sentence[i]==origin){toggle =true; break;}
if(islower(sentence[i])){return "invalid";}
i++;
}
if(toggle==false){return "invalid";}
str = sentence.substr(1,i-1);
}
else if(expect==3){
char ruletwo = sentence[0];
char ruleone = sentence[2];
if(m[ruleone]==true){ return "invalid";}
else{m[ruleone]=true;}
if(m[ruletwo]==true){ return "invalid";}
else{m[ruletwo]=true;}
int i=2;
bool toggle = false;
while(i<sentence.size()){
// cout<<sentence<<endl;
if(sentence[i]!=ruleone){
// cout<<sentence[i]<<endl;
// cout<<ruletwo<<endl;
if(sentence[i]==ruletwo){
toggle = true;
break;
}
else{
return "invalid";
}
}
sentence.erase(i,1);
if(islower(sentence[i])){return "invalid";}
i++;}
if(toggle==false){return "invalid";}
str = sentence.substr(1,i-1);
//cout<<str;
}
if(expect==2 || expect==3){
sentence = sentence.substr(str.size()+2);
}
else{
sentence = sentence.substr(str.size());
}
if(t==0){answer =str;}
else{
answer=answer+" "+str;
}
t++;
}
return answer;
}
미해결 문제입니다.
'프로그래머스 > 그래프' 카테고리의 다른 글
배달 - 다익스트라 알고리즘 (0) | 2021.03.04 |
---|---|
합승 택시 요금 (0) | 2021.03.02 |
순위 - 플로이드 와샬 (0) | 2021.02.15 |
가장 먼 노드 (0) | 2021.02.14 |