UVA Solution 464 – Sentence.Phrase Generator – Solution in C++

UVA Onilne Judge Solution UVA-Volume 4

UVA Solution 464 – Sentence.Phrase Generator – Solution in C++

UVA Online Judge Solution 464 – Sentence.Phrase Generator | Volume 4
UVA Problem Link – 464 – Sentence.Phrase Generator

Problem Name: 464 – Sentence.Phrase Generator
Problem Number : UVA – 464 – Sentence.Phrase Generator
Online Judge : UVA Online Judge Solution
Volume: 4
Solution Language : C plus plus

UVA Online Judge Solution, UVA OJ Solution list, UVA Problems Solution, UVA solver, UVA all problem solution list

UVA Solution 464 – Sentence.Phrase Generator  Code in CPP:

#include <stdio.h>
#include <vector>
#include <iostream>
#include <sstream>
#include <string.h>
#include <stack>
using namespace std;
struct
data {
vector<vector<int> > KIND1;
vector<vector<string> > KIND2;
};

char
input[] = "1 1 2n1 1 3n2 4 4 9 5 11n3 3 4 10 11n4 1 6n
5 1 6n6 2 15 7n7 1 12n7 2 8 12n8 1 16n8 2 17 16n9 1 13n
9 2 17 13n10 1 14n10 2 17 14n11 2 18 6n11 1 19n12 1 mann
12 1 dogn12 1 fishn12 1 computern12 1 wavesn13 1 struck 13 1 sawn
13 1 bitn13 1 tookn14 1 sleptn14 1 jumpedn14 1 walkedn
14 1 swamn15 1 then15 1 an16 1 greenn16 1 smalln16 1 rabidn
16 1 quickn17 1 nearlyn17 1 suddenlyn17 1 restlesslyn
18 1 onn18 1 overn18 1 throughn"
;
char
start[20][20] = {
""
, "sentence", "trans-sentence", "intrans-sentence",
"subject"
, "object", "noun-phrase", "modified-noun",
"modifier"
, "verb-phrase", "intrans-verb-phrase",
"prep-phrase"
, "noun", "trans-verb", "intrans-verb",
"article"
, "adjective", "adverb", "preposition", "empty"
};

/*
<sentence> 1>(2 | 3)
<trans-sentence> 2>(4 9 5 11)
<intrans-sentence> 3>(4 10 11)
<subject> 4>(6)
<object> 5>(6)
<noun-phrase> 6>(15 7)
<modified-noun> 7>(12 | 8 12)
<modifier> 8>(16 | 17 16)
<verb-phrase> 9>(13 | 17 13)
<intrans-verb-phrase> 10>(14 | 17 14)
<prep-phrase> 11>(18 6 | 19)
<noun> 12> man | dog | fish | computer | waves
<trans-verb> 13> struck | saw | bit | took
<intrans-verb> 14> slept | jumped | walked | swam
<article> 15> the | a
<adjective> 16>green | small | rabid | quick
<adverb> 17>nearly | suddenly | restlessly
<preposition> 18>on | over | through
<empty> 19>""
*/

int
main() {
data CH[20];
int
n, m, num;
stringstream sin(input);
while
(sin >> n) {
sin >> m;
vector<int> buf1;
vector<string> buf2;
string token;
while
(m--) {
sin >> token;
if
(token[0] >= '0' && token[0] <= '9') {
stringstream nin(token);
nin >> num;
buf1.push_back(num);
}
else {
buf2.push_back(token);
}
}

if
(buf1.size())
CH[n].KIND1.push_back(buf1);
if
(buf2.size())
CH[n].KIND2.push_back(buf2);
}

vector<string> buf2;
buf2.push_back("");
CH[19].KIND2.push_back(buf2);
int
k = 0, i, j;
char
s[50];
while
(scanf("%s", s) == 1) {
int
st = 0, x, y;
for
(i = 1; i <= 19; i++) {
if
(!strcmp(start[i], s))
st = i, i = 20;
}

stack<int> stk;
stk.push(st);
int
spaceflag = 0;
while
(!stk.empty()) {
x = stk.top(), stk.pop();
if
(CH[x].KIND1.size()+CH[x].KIND2.size() > 1)
k++;
if
(CH[x].KIND1.size() == 0) {
y = k%CH[x].KIND2.size();
if
(spaceflag && CH[x].KIND2[y][0].length()) putchar(' ');
if
(CH[x].KIND2[y][0].length())
spaceflag = 1;
printf("%s", CH[x].KIND2[y][0].c_str());
}
else {
y = k%CH[x].KIND1.size();
//printf("ch %dn", y);
for(j = CH[x].KIND1[y].size()-1; j >= 0; j--)
stk.push(CH[x].KIND1[y][j]);
}
}

printf("n");
}

return
0;
}

Tags: UVA Online Judge Solution, UVA OJ Solution list, UVA Problems Solution, UVA solver, UVA all problem solution list, UVA 464 code in C, UVA Sentence.Phrase Generator code in C++, UVA 464 – Sentence.Phrase Generator solution in C, UVA 464 – Sentence.Phrase Generator solution

Leave a Reply

Your email address will not be published.