-
Notifications
You must be signed in to change notification settings - Fork 0
/
Finding-alt-letters.cpp
87 lines (79 loc) · 1.49 KB
/
Finding-alt-letters.cpp
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
/* Given a word, the program lists all the pairs of alternating letters in the context of word-representable graphs. */
#include <bits/stdc++.h>
using namespace std;
int main() {
int k, m, n;
bool flag;
string Word, UniqueW = "";
cin>>Word;
unordered_set<char>H;
cout << "\nEnter the word: ";
for(auto ch:Word)
{
if(H.find(ch)==H.end())
{
UniqueW += ch;
H.insert(ch);
}
}
int size = UniqueW.length();
vector<vector<int> > vec(size, vector<int> (size, 0)); //adjacency matrix
cout << "\nThe alternating letters are:\n";
for(int i=0; i<size; i++)
{
for(int j=i+1; j<size; j++)
{
k = 0;
m = 0;
n = 1;
flag = 1;
while(Word[k]!='\0') {
if(Word[k]==UniqueW[i] && n==1)
{
m = 1;
n = 0;
}
else if(Word[k]==UniqueW[j] && m==1)
{
n = 1;
m = 0;
}
else if(Word[k]==UniqueW[i] || Word[k]==UniqueW[j])
{
flag = 0;
break;
}
++k;
}
if(flag==1)
{
cout << UniqueW[i] << "--" << UniqueW[j] << endl;
vec[i][j] = 1;
vec[j][i] = 1;
}
}
}
cout << "\nThe adjacency matrix is:\n";
for(int i = -1; i < size; i++)
{
if(i==-1)
{
cout<<" ";
for(char c:UniqueW)
{
cout << c << " ";
}
cout << endl;
}
else
{
cout << UniqueW[i] << " ";
for(int j = 0; j < size; j++)
{
cout << vec[i][j] << " ";
}
cout << endl;
}
}
return 0;
}