-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathLeftFactoring.cpp
117 lines (103 loc) · 2.28 KB
/
LeftFactoring.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long int i, j, k, l, n, m = 9999999999, mini, ma = 0;
string s[100], st, ch, sc = "", result, fs, maxi, rs = "";
vector<string> ss;
vector<string> sp;
cout << "Enter the number of productions:";
cin >> n;
cout << "Enter the productions:\n";
for (i = 1; i <= n; i++)
{
cin >> s[i];
}
for (i = 1; i <= n; i++)
{
st = s[i];
sc = "";
for (j = 0; j < st.length(); j++)
{
if (i == 1)
{
fs = st[0];
}
if (st[j] == '=')
{
l = j;
}
}
if (i == 1)
{
for (k = l + 1; k < st.length(); k++)
{
if (st[k] == '|')
{
ss.push_back(sc);
sc = "";
}
if (st[k] != '|')
{
ch = st[k];
sc = sc + ch;
}
}
ss.push_back(sc);
}
// cout<<sc<<endl;
}
for (k = 0; k < ss.size(); k++)
{
mini = ss[k].size();
m = min(m, mini);
maxi = ss[k];
// cout<<ss[k]<<endl;
}
// cout<<maxi<<endl;
for (k = 0; k < m; k++)
{
// cout<<ss[0][k]<<endl;
}
for (int i = 0; i < m; i++)
{
char current = ss[0][i];
for (int j = 1; j < ss.size(); j++)
{
if (ss[j][i] != current)
{
break;
}
result.push_back(current);
}
}
for (j = 0; j < ss.size(); j++)
{
maxi = ss[j];
// cout<<maxi<<result.length()<<endl;
for (k = 0; k < maxi.length(); k++)
{
if (k >= result.length())
{
rs = rs + maxi[k];
}
// cout<<rs<<endl;
}
if (j != ss.size() - 1)
{
rs = rs + '|';
}
}
cout << fs << "=" << result << fs << "'" << endl;
cout << fs << "'"
<< "=" << rs << endl;
for (i = 2; i <= n; i++)
{
cout << s[i] << endl;
}
return 0;
}