Programming/알고리즘 문제 풀이와 팁

1. 뒤죽박죽 문자열에서 단어 사이의 알파벳 갯수 구하기

KingSSSSS 2018. 6. 25. 15:34
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm> //transform 사용
#include <functional> //bind1st
#include <vector>
#include <stack>
#include <iostream>
 
using namespace std;
 
#define STRNUM 6
 
bool Check(vector<char>& cccc)
{
    if(cccc.size() < STRNUM-1return false;
 
    int iiii=0;
    bool _is=false;
    char apple[STRNUM] = "apple";
 
    vector<char> _temp = cccc;
 
 
    for (int i = 0; i < STRNUM-1; i++)
    {
        for (int j = 0; j < _temp.size(); j++)
        {
            if (_temp[j] != 0)
            {
                if (_temp[j] == apple[i])
                {
                    iiii++;
                    _temp[j] = 0;
                    apple[i] = 0;
                }
            }
        }
    }
    if(iiii >= STRNUM-1) _is = true;
 
    return _is;
}
 
int main()
{
    // apple
    string inputStr1 = "apwpeleasappfsle";// 2
    string inputStr2 = "applesappleaapplewapple";//3
    string inputStr3 = "papelaapple";//1
    string inputStr4 = "ppalewqeapple";//3
 
    string inputStr5 = "epdpladfpwpaaxlxqefrappplerappawele";//5
    //          epdpla df pwpaaxlxqe fr appple r appawele
 
    vector< char > cccc;
    vector< int > valwww;
    string _output = "";
    char apple[STRNUM] = "apple";
    for (int j = 0; j < inputStr5.length(); j++)
    {
        for (int i = 0; i < STRNUM-1; i++)
        {
            char dssdd = inputStr5[j];
            if (apple[i] == dssdd)
            {
                cccc.push_back(dssdd);
                valwww.push_back(j);
                i = STRNUM-1;
            }
        }
        if( Check(cccc) ){
            int _min = inputStr5.length()+1,_max = 0;
            for (int i = 0; i < valwww.size(); i++)
            {
                if(_min > valwww[i])_min = valwww[i];
                if(_max < valwww[i])_max = valwww[i];
            }
 
            _output += inputStr5.substr(0,_min);
            string dsf = inputStr5.substr(_max+1,inputStr5.length());
            inputStr5 = dsf;
            valwww.clear();
            cccc.clear();
            j = -1;
        }
    }
    printf"%s\n" , _output.c_str() );
    return 1;
}
cs