Submission #4016605


Source Code Expand

#include <bits/stdc++.h>
#define FOR(v, a, b) for(int v = (a); v < (b); ++v)
#define FORE(v, a, b) for(int v = (a); v <= (b); ++v)
#define REP(v, n) FOR(v, 0, n)
#define REPE(v, n) FORE(v, 0, n)
#define REV(v, a, b) for(int v = (a); v >= (b); --v)
#define ALL(x) (x).begin(), (x).end()
#define ITR(it, c) for(auto it = (c).begin(); it != (c).end(); ++it)
#define RITR(it, c) for(auto it = (c).rbegin(); it != (c).rend(); ++it)
#define EXIST(c,x) ((c).find(x) != (c).end())
#define LLI long long int
#define fst first
#define snd second
#ifdef DEBUG
#include <boost/core/demangle.hpp>
#define dump(x) cerr << "L" << __LINE__ << ": in " << __PRETTY_FUNCTION__ << " \e[32;1m" << boost::core::demangle(typeid(x).name()) << "\e[37m" << " " << (#x) << " = " << (x) << "\e[m" << endl;
#else
#define dump(x)
#endif
using namespace std;
template <typename T> using V = vector<T>;
template <typename T, typename U> using P = pair<T,U>;
template <typename I> void join(ostream &ost, I s, I t, string d=" "){for(auto i=s; i!=t; ++i){if(i!=s)ost<<d; ost<<*i;}ost<<endl;}
template <typename T> istream& operator>>(istream &is, vector<T> &v){for(auto &a : v) is >> a; return is;}
template <typename T, typename U> istream& operator>>(istream &is, pair<T,U> &p){is >> p.first >> p.second; return is;}

template <typename T> ostream& operator<<(ostream &os, const vector<T> &v){os << "{";ITR(i,v){if(i!=v.begin())os << ","; os << *i;}os << "}"; return os;}
template <typename T, typename U> ostream& operator<<(ostream& os, const pair<T,U> &p){os << "(" << p.first << "," << p.second << ")"; return os;}

template <typename T> T& chmin(T &a, const T &b){return a = min(a,b);}
template <typename T> T& chmax(T &a, const T &b){return a = max(a,b);}

bool starts_with(const string &str, const string &prefix){
  return str.size() >= prefix.size() && equal(ALL(prefix),str.begin());
}


class TopologicalSort{
public:
  static bool dfs(int cur, vector<vector<int>> &graph, vector<int> &visit, vector<int> &ret){
    if(visit[cur] == -1) return false;
    if(visit[cur] == 0){
      visit[cur] = -1;
      for(auto &next : graph[cur]) if(!dfs(next,graph,visit,ret)) return false;
      visit[cur] = 1;
      ret.push_back(cur);
    }
    return true;
  }
  
  static bool tsort_dfs(vector<vector<int>> &graph, vector<int> &ret){
    int n = graph.size();
    vector<int> visit(n,0);
    REP(i,n) if(visit[i] == 0) if(!TopologicalSort::dfs(i,graph,visit,ret)) return false;
    reverse(ALL(ret));
    return true;
  }

  static bool tsort_priority_queue(vector<vector<int>> graph, vector<int> &ret){
    int n = graph.size();
    vector<unordered_set<int>> in_graph(n);
    REP(i,n){
      for(auto j : graph[i]){
	in_graph[j].insert(i);
      }
    }
    priority_queue<int,vector<int>,greater<int>> q;
    REP(i,n){
      if(in_graph[i].empty()) q.push(i);
    }

    while(!q.empty()){
      int cur = q.top();
      ret.push_back(cur); q.pop();
      for(auto next : graph[cur]){
	in_graph[next].erase(cur);
	if(in_graph[next].empty()){
	  q.push(next);
	}
      }
    }
    return ret.size()==n;
  }
};


bool edges[26][26];

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N; cin >> N;

  vector<vector<int>> graph(26);

  map<char,int> char_int;
  map<int,char> int_char;
  REP(i,26){
    char_int['a'+i] = i;
    int_char[i] = 'a'+i;
  }
  
  REP(_,N){
    string a,b; cin >> a >> b;

    if(a.size() > b.size() && starts_with(a,b)){
      cout << -1 << endl;
      return 0;
    }

    if(starts_with(b,a)) continue;

    REP(i,min(a.size(),b.size())){
      if(a[i] != b[i]){
	graph[char_int[a[i]]].push_back(char_int[b[i]]);
	edges[char_int[a[i]]][char_int[b[i]]] = true;
	break;
      }
    }
  }

  dump(graph);

  vector<int> ret;
  if(!TopologicalSort::tsort_priority_queue(graph,ret)){
    cout << -1 << endl;
    return 0;
  }

  string ans;
  REP(i,26) ans.push_back(int_char[ret[i]]);
  
  cout << ans << endl;
  
  return 0;
}

Submission Info

Submission Time
Task C - 山田山本問題
User you070707
Language C++14 (GCC 5.4.1)
Score 0
Code Size 4077 Byte
Status WA
Exec Time 2111 ms
Memory 542048 KB

Judge Result

Set Name All
Score / Max Score 0 / 600
Status
AC × 24
WA × 10
TLE × 11
Set Name Test Cases
All 00_sample_01, 00_sample_02, 00_sample_03, 00_sample_04, 01_manual_00, 01_manual_01, 01_manual_02, 01_manual_03, 01_manual_04, 01_manual_05, 01_manual_06, 01_manual_07, 10_random_00, 10_random_01, 10_random_02, 10_random_03, 10_random_04, 10_random_05, 10_random_06, 10_random_07, 10_random_08, 10_random_09, 30_random_00, 30_random_01, 30_random_02, 30_random_03, 30_random_04, 30_random_05, 30_random_06, 30_random_07, 30_random_08, 30_random_09, 50_random_00, 50_random_01, 50_random_02, 50_random_03, 50_random_04, 50_random_05, 50_random_06, 50_random_07, 50_random_08, 50_random_09, 51_random_00, 51_random_01, 51_random_02
Case Name Status Exec Time Memory
00_sample_01 AC 1 ms 256 KB
00_sample_02 AC 1 ms 256 KB
00_sample_03 AC 1 ms 256 KB
00_sample_04 AC 1 ms 256 KB
01_manual_00 AC 1 ms 256 KB
01_manual_01 WA 1 ms 256 KB
01_manual_02 WA 1 ms 256 KB
01_manual_03 AC 1 ms 256 KB
01_manual_04 AC 1 ms 256 KB
01_manual_05 AC 1 ms 256 KB
01_manual_06 AC 1 ms 256 KB
01_manual_07 TLE 2105 ms 542048 KB
10_random_00 AC 1 ms 256 KB
10_random_01 WA 1 ms 256 KB
10_random_02 WA 1 ms 256 KB
10_random_03 WA 1 ms 256 KB
10_random_04 WA 1 ms 256 KB
10_random_05 WA 1 ms 256 KB
10_random_06 WA 1 ms 256 KB
10_random_07 AC 1 ms 256 KB
10_random_08 WA 1 ms 256 KB
10_random_09 WA 1 ms 256 KB
30_random_00 AC 1 ms 256 KB
30_random_01 AC 1 ms 256 KB
30_random_02 AC 1 ms 256 KB
30_random_03 AC 1 ms 256 KB
30_random_04 AC 1 ms 256 KB
30_random_05 AC 1 ms 256 KB
30_random_06 AC 1 ms 256 KB
30_random_07 AC 1 ms 256 KB
30_random_08 AC 1 ms 256 KB
30_random_09 AC 1 ms 256 KB
50_random_00 TLE 2104 ms 102880 KB
50_random_01 TLE 2103 ms 54988 KB
50_random_02 TLE 2104 ms 56396 KB
50_random_03 TLE 2104 ms 89688 KB
50_random_04 TLE 2104 ms 98020 KB
50_random_05 TLE 2111 ms 97636 KB
50_random_06 TLE 2104 ms 251476 KB
50_random_07 TLE 2104 ms 164704 KB
50_random_08 TLE 2104 ms 95844 KB
50_random_09 TLE 2104 ms 165100 KB
51_random_00 AC 3 ms 256 KB
51_random_01 AC 1 ms 256 KB
51_random_02 AC 3 ms 256 KB