Submission #4016668


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 tsort_priority_queue(vector<vector<int>> graph, vector<int> &ret){
    int n = graph.size();
    vector<int> indeg(n);
    REP(i,n){
      for(auto j : graph[i]){
	++indeg[j];
      }
    }
    priority_queue<int,vector<int>,greater<int>> q;
    REP(i,n){
      if(indeg[i]==0) q.push(i);
    }

    while(!q.empty()){
      int cur = q.top(); q.pop();
      ret.push_back(cur);
      for(auto next : graph[cur]){
	--indeg[next];
	if(indeg[next]==0){
	  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)){ // a=yamamoto, b=yama の様な場合 yamamoto < yama は不可能。
      cout << -1 << endl;
      return 0;
    }

    if(starts_with(b,a)) continue; // a=yama, b=yamada の様な場合、常に yama < yamada である。

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

  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 3550 Byte
Status WA
Exec Time 34 ms
Memory 256 KB

Judge Result

Set Name All
Score / Max Score 0 / 600
Status
AC × 24
WA × 21
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 AC 1 ms 256 KB
01_manual_02 AC 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 WA 19 ms 256 KB
10_random_00 WA 2 ms 256 KB
10_random_01 WA 2 ms 256 KB
10_random_02 WA 2 ms 256 KB
10_random_03 WA 2 ms 256 KB
10_random_04 WA 2 ms 256 KB
10_random_05 WA 2 ms 256 KB
10_random_06 WA 2 ms 256 KB
10_random_07 WA 2 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 WA 31 ms 256 KB
50_random_01 WA 30 ms 256 KB
50_random_02 WA 34 ms 256 KB
50_random_03 WA 32 ms 256 KB
50_random_04 WA 29 ms 256 KB
50_random_05 WA 33 ms 256 KB
50_random_06 WA 28 ms 256 KB
50_random_07 WA 34 ms 256 KB
50_random_08 WA 29 ms 256 KB
50_random_09 WA 33 ms 256 KB
51_random_00 AC 20 ms 256 KB
51_random_01 AC 2 ms 256 KB
51_random_02 AC 13 ms 256 KB