Skip to content

Instantly share code, notes, and snippets.

@saurav3199
Last active January 16, 2020 13:43
Show Gist options
  • Save saurav3199/943352e50501e1dd4052396d10839ac9 to your computer and use it in GitHub Desktop.
Save saurav3199/943352e50501e1dd4052396d10839ac9 to your computer and use it in GitHub Desktop.
Checking if the list of given codes can be used to decode any possible string uniquely.
// the algorithm is known as Sardinas–Patterson algo.
#include <bits/stdc++.h>
using namespace std;
// in-short-use macros
#define ll long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
//container-use
typedef vector< ll > vl;
typedef vector< vl > vvl;
typedef pair< ll,ll > pll;
typedef vector<pair<ll,ll > > vpl;
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define rep(i,a,b) for (int i = a; i <= b; i++)
//main function here
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
// freopen("C:/Users/Saurav Kumar Singh/Desktop/cp/input.txt", "r", stdin);
// freopen("C:/Users/Saurav Kumar Singh/Desktop/cp/output.txt", "w", stdout);
#endif
cout<<"-----Welcome to the unique decoding system-----\n\n";
string s;
cout<<"Enter the number of codes: \n";
int n;
cin>>n;
set<string> se;
queue<string> q;
for(int i=0;i<n;i++)
{
cin>>s;
se.insert(s);
q.push(s);
}
bool ans=true;
while(!q.empty())
{
string h=q.front(),ch="";
for(int i=0;i<h.size()-1;i++)
{
ch+=h[i];
if(se.find(ch)!=se.end())
{
string f=h.substr(i+1,h.size());
if(se.find(f)!=se.end())
{
ans=false;
break;
}
else
{
se.insert(f); // dangling suffix added
q.push(f);
}
}
}
q.pop();
if(!ans)
break;
}
if(!ans)
cout<<"Not Decoded uniquely\n";
else
cout<<"Decoded uniquely possible\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment