c++ - Sorting criteria for ordering this words? -


i want 2 types of sorting, first 1 follow:

given set of distinct lines 2 words separate space, order them in way next line has first word equal last word of previous line. example:

  • pamela luisa
  • luis angel
  • pedro luis
  • luisa elianny
  • angel pedro
  • elianny pamela

should give like:

  • luisa elianny
  • elianny pamela
  • pamela luisa
  • pedro luis
  • luis angel
  • angel pedro

i read every line string, put inside vector , used sort comparison function this:

bool cmp(std::string a, std::string b) {     std::string prb = a.substr(a.find(" ")+1),                 prb2 = b.substr(0, b.find(" "));      return prb == prb2; } 

but desire result have sort twice. don't understand why happens.

the second ordering criteria is:

given set of words order next word has same first letter last letter of previous word. example:

  • eo
  • uiu
  • aeiou

should give like:

  • aeiou
  • uiu
  • eo

i did this, doesn't seems work properly.

bool cmp(std::string a, std::string b) {     if(b[b.size()-1] == a[0] && a[0] != b[0]) return false;     return b[b.size()-1] == a[0]; } 

how can make 2 sorting works fine?

first, problem #2 can reduced problem #1. replace each string string consisting of first letter , last letter, separated space; e.g. aeiou becomes a u. it's clear solving problem #1 on these new strings give solution problem #2 on original strings.

second, problem of finding hamiltonian path in directed graph. graph in question consists of input strings vertices, edge drawn between every pair of vertices of form x y , y z.

the problem, stated, underspecified: it's not clear considered right answer when graph doesn't contain hamiltonian path. example has pamela luisa followed pedro luis, doesn't satisfy requirement. 1 possible interpretation of finding minimum path cover.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -