Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <cstring>
- using namespace std;
- ifstream fin ("regex.in");
- ofstream fout ("regex.out");
- int n, q0, i, len, nod, auxnod, j;
- char s[1010];
- void stea(int &i) {
- fout << nod << " . " << nod + 1 << "\n";
- fout << nod << " . " << nod + 3 << "\n";
- fout << nod + 1 << ' ' << s[i] << ' ' << nod + 2 << "\n";
- fout << nod + 2 << " . " << nod + 3 << "\n";
- fout << nod + 3 << " . " << nod + 1 << "\n";
- i += 2;
- nod += 3;
- }
- void normal(int &i) {
- fout << nod << ' ' << s[i] << ' ' << nod + 1 << "\n";
- i++;
- nod++;
- }
- int main() {
- fin >> s;
- nod = 1;
- q0 = 1;
- i = 0;
- len = strlen(s);
- while (i < len) {
- if ('a' <= s[i] && s[i] <= 'z') {
- if (s[i + 1] == '*') {
- stea(i);
- } else {
- normal(i);
- }
- } else {
- i++;
- j = i;
- while (s[j] != ')' && s[j] != '|') {
- j++;
- }
- if (s[j] == ')') { // cazul cu * la final de paranteza
- auxnod = nod;
- fout << nod << " . " << nod + 1 << "\n";
- nod++;
- while (i < j) {
- if ('a' <= s[i] && s[i] <= 'z') {
- if (s[i + 1] == '*') {
- stea(i);
- } else {
- normal(i);
- }
- }
- }
- fout << nod << " . " << nod + 1 << "\n";
- nod++;
- fout << auxnod << " . " << nod << "\n";
- fout << nod << " . " << auxnod + 1 << "\n";
- i += 2;
- } else { // cazul |
- while (s[j] != ')') {
- j++;
- }
- if (s[j + 1] == '*') { // * dupa paranteza cu |
- int cap1;
- auxnod = nod;
- fout << nod << " . " << nod + 1 << "\n";
- nod++;
- while (s[i] != '|') {
- if (s[i + 1] == '*') {
- stea(i);
- } else {
- normal(i);
- }
- }
- fout << nod << " . " << auxnod << "\n";
- i++;
- fout << auxnod << " . " << nod + 1 << "\n";
- nod++;
- while (s[i] != ')') {
- if (s[i + 1] == '*') {
- stea(i);
- } else {
- normal(i);
- }
- }
- fout << nod << " . " << auxnod << "\n";
- nod++;
- i += 2;
- fout << auxnod << " . " << nod << "\n";
- } else { // fara * dupa paranteza cu |
- auxnod = nod;
- while (s[i] != '|') {
- if (s[i + 1] == '*') {
- stea(i);
- } else {
- normal(i);
- }
- }
- i++;
- if (s[i + 1] == '*') {
- if (s[i + 2] == ')') {
- fout << auxnod << " . " << nod + 1 << "\n";
- fout << auxnod << " . " << nod << "\n";
- fout << nod + 1 << ' ' << s[i] << ' ' << nod + 2 << "\n";
- fout << nod + 2 << " . " << nod << "\n";
- fout << nod << " . " << nod + 1 << "\n";
- i += 2;
- auxnod = nod;
- nod += 3;
- } else {
- fout << auxnod << " . " << nod + 1 << "\n";
- fout << auxnod << " . " << nod + 3 << "\n";
- fout << nod + 1 << ' ' << s[i] << ' ' << nod + 2 << "\n";
- fout << nod + 2 << " . " << nod + 3 << "\n";
- fout << nod + 3 << " . " << nod + 1 << "\n";
- i += 2;
- auxnod = nod;
- nod += 3;
- }
- } else {
- if (s[i + 1] == ')') {
- fout << auxnod << ' ' << s[i] << ' ' << nod << "\n";
- auxnod = nod;
- i++;
- } else {
- fout << auxnod << ' ' << s[i] << ' ' << nod + 1 << "\n";
- auxnod = nod;
- nod++;
- i++;
- }
- }
- if (s[i] == ')') {
- i++;
- } else {
- while (s[i + 1] != ')') {
- if (s[i + 1] == '*') {
- stea(i);
- } else {
- normal(i);
- }
- }
- fout << nod << ' ' << s[i] << ' ' << auxnod << "\n";
- fout << auxnod << " . " << nod + 1 << "\n";
- nod++;
- i += 2;
- }
- }
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement