Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <fstream>
- using namespace std;
- ifstream f("minerale.in");
- ofstream g("minerale.out");
- string a,b,s;
- int n;
- map <string, set<string> > terminal;
- map <string, set<string> > non_terminal;
- set <string> d[105][105];
- map <string,tuple<int,int,string,int,int,string> >mp[105][105];
- queue < tuple<int,int,string,int> >coada;
- map <tuple<int, int, string>, vector<tuple<int, int, string> > > v;
- void dfs(tuple<int, int, string> s, tuple<int,int,string,int,int,string> a, string OP) {
- v[s].push_back(make_tuple(get<0>(a), get<1>(a), get<2>(a)));
- v[s].push_back(make_tuple(get<3>(a), get<4>(a), get<5>(a)));
- tuple<int, int, string> left = make_tuple(get<0>(a), get<1>(a), get<2>(a));
- tuple<int, int, string> right = make_tuple(get<3>(a), get<4>(a), get<5>(a));
- string name_left = get<2>(a);
- string name_right = get<5>(a);
- if (get<0>(a) == 1)
- v[left].push_back(make_tuple(-1, -1, name_left));
- else dfs(left, mp[get<0>(a)][get<1>(a)][name_left], OP);
- if (get<3>(a) == 1)
- v[right].push_back(make_tuple(-1, -1, name_right));
- else dfs(right, mp[get<3>(a)][get<4>(a)][name_right], OP);
- }
- bool cyk(string s)
- {
- int i,j,k;
- for(i=1;i<=n;i++)
- {
- string aux="";
- aux+=s[i-1];
- if(!terminal[aux].empty())
- {
- d[1][i]=terminal[aux];
- }
- }
- for(i=2;i<=n;i++)
- {
- for(j=1;j<=n-i+1;j++)
- {
- for(k=1;k<=i-1;k++)
- {
- for(auto it1 : d[k][j])
- {
- for(auto it2 : d[i-k][j+k])
- {
- string aux="";
- aux+=it1;
- aux+=it2;
- for(auto it3: non_terminal[aux])
- {
- d[i][j].insert(it3);
- mp[i][j][it3]=make_tuple(k,j,it1,i-k,j+k,it2);
- }
- }
- }
- }
- }
- }
- /*for(i=1;i<=n;i++)
- {
- for(j=1;j<=n-i+1;j++)
- {
- cout<<"[";
- for(auto it : d[i][j])
- {
- cout<<it<<" ";
- }
- cout<<"] ";
- }
- cout<<"\n";
- }
- */
- //string aux="";
- //aux+='S';
- //cout<<get<0>(mp[n][1][aux])<<" "<<get<1>(mp[n][1][aux])<<" "<<get<2>(mp[n][1][aux])<<" "<<get<3>(mp[n][1][aux])<<" "<<get<4>(mp[n][1][aux])<<" "<<get<5>(mp[n][1][aux]);
- if(d[n][1].find("S")!=d[n][1].end())
- {
- dfs(make_tuple(n, 1, "S"), mp[n][1]["S"], s);
- return 1;
- }
- else
- {
- return 0;
- }
- }
- int main()
- {
- int i;
- f>>n;
- for(i=1;i<=n;i++)
- {
- f>>a>>b;
- if(b.size()==2)
- {
- non_terminal[b].insert(a);
- }
- else
- {
- terminal[b].insert(a);
- }
- }
- f>>s;
- n=s.size();
- if(cyk(s)==0)
- {
- cout<<"Cuvantul nu poate fi generat de gramatica";
- }
- else
- {
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement