-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRebuild_Alphabet.cpp
More file actions
98 lines (91 loc) · 1.75 KB
/
Rebuild_Alphabet.cpp
File metadata and controls
98 lines (91 loc) · 1.75 KB
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
91
92
93
94
95
96
97
98
#include "stdafx.h"
#include "iostream"
#include "vector"
#include "string"
#include "map"
#include <algorithm>
#include <stdlib.h>
#include <time.h>
#include <stack>
#include <sstream>
#include <iomanip>
#include <random>
#include <assert.h>
#include <iterator>
#include <queue>
#include <math.h>
#include <vector>
using namespace std;
struct Node
{
int prev[26];
int prevNumber;
Node()
{
prevNumber=0;
for(int i=0;i<26;i++)prev[i]=0;
}
};
string rebuildAlphabet(vector<string> s)
{
Node nodes[26];
for(int i=0;i<s.size();i++)
{
string ts=s[i];
if(ts[1]!=ts[2]&&nodes[ts[1]-'a'].prev[ts[0]-'a']==0)
{
nodes[ts[1]-'a'].prev[ts[0]-'a']=1;
nodes[ts[1]-'a'].prevNumber++;
}
if(ts[2]!=ts[0]&&nodes[ts[2]-'a'].prev[ts[0]-'a']==0)
{
nodes[ts[2]-'a'].prev[ts[0]-'a']=1;
nodes[ts[2]-'a'].prevNumber++;
}
if(ts[2]!=ts[1]&&nodes[ts[2]-'a'].prev[ts[1]-'a']==0)
{
nodes[ts[2]-'a'].prev[ts[1]-'a']=1;
nodes[ts[2]-'a'].prevNumber++;
}
}
string result;
for(int i=0;i<26;i++)
{
for(int j=0;j<26;j++)
{
if(nodes[j].prevNumber==i)
{
result.push_back('a'+j);
break;
}
}
}
return result;
}
vector<string> buildSubstr(string s)
{
vector<string> result;
for(int i=0;i<24;i++)
for(int j=i+1;j<25;j++)
for(int k=j+1;k<26;k++)
{
string tmp;
tmp.push_back(s[i]);
tmp.push_back(s[j]);
tmp.push_back(s[k]);
result.push_back(tmp);
}
return result;
}
void testRebuildAlphabet()
{
string s="abcdefghijklmnopqrstuvwxyz";
vector<string> newSubstr=buildSubstr(s);
cout<<s<<endl;
cout<<rebuildAlphabet(newSubstr)<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
testRebuildAlphabet();
return 0;
}