I implemented the solution with backtracking, but it got runtime error. Can 
anyone please guide me on what's wrong with it ?:

#include<bits/stdc++.h>
#define rep(i, a, b) for(int i=a; i<b; i++)
#define repi(i, a, b) for(int i=a; i>b; i--)
#define db(x) (cerr << #x << ": " << x << '\n')
#define sync ios_base::sync_with_stdio(false), cin.tie(NULL)
//#define int long long
#define ll long long
#define mp make_pair
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll, ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define fi first
#define se second
#define umap unordered_map
#define umapi umap<int, int>
#define mapi map<int, int>
#define uset unordered_set
#define useti uset<int>
#define seti set<int, int>
#define pqueue priority_queue
#define si(a) scanf("%d", &a)
//#define sll(a) scanf("%lld", &a)
#define bitcount(x) __builtin_popcount(x)
#define N 2003
#define L 13
using namespace std;

int n, l;
string ans;
set<char> adj[L];
set<string> pre[L];

string s[L];

void prep()
{
        rep(i, 0, l){
                adj[i].clear();
                pre[i].clear();
        }
        
        rep(i, 0, n){
                rep(j, 1, l+1){
                        pre[j-1].insert(s[i].substr(0, j));
                        adj[j-1].insert(s[i][j-1]);
                }
        }
}

bool bt(int i)
{
        if(i == l)
                return 0;
        
        for(char c : adj[i]){
                ans.push_back(c);
                if(pre[i].find(ans) == pre[i].end()){
                        return 1;
                }
                if(bt(i+1))
                        return 1;
                ans.pop_back();
        }
        return 0;
}

main()
{
        int t, oo = 1;
        cin >> t;
        while(t--){
                cin >> n >> l;
                rep(i, 0, n)
                        cin >> s[i];
                prep();
                
                ans.clear();
                cout << "Case #" << oo++ << ": ";       
                if(bt(0)){
                        cout << ans;
                        rep(i, ans.length(), l)
                                cout << s[0][i];
                }
                else
                        cout << '-';
                cout << '\n';
        }
}

-- 
You received this message because you are subscribed to the Google Groups 
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-code+unsubscr...@googlegroups.com.
To post to this group, send email to google-code@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-code/2cb9f4be-e929-4125-9ef8-6952cbc8647e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to