fork download
  1. #include <iostream>
  2. #include <set>
  3. #include <map>
  4. #include <stack>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. string t;
  10. int n,m;
  11. set <string> words;
  12. map <string, string> mymap;
  13.  
  14. stack <string> ans;
  15. int mz = 0;
  16.  
  17. string lower(string s){
  18. int len = s.size();
  19. for(int i = 0; i<len; i++){
  20. if(s[i]<'a') s[i] = s[i] - 'A' + 'a';
  21. }
  22. return s;
  23. }
  24.  
  25. bool sol(string s){
  26. if(s=="") return true;
  27. string z = "";
  28. int len = s.size();
  29. for(int i = 0; i<min(mz,len); i++){
  30. z += s[i];
  31. if(words.find(z)!=words.end()){
  32. ans.push(z);
  33. if(sol(s.substr(i+1,len-i))){
  34. while(!ans.empty()){
  35. cout<<mymap[ans.top()]<<" ";
  36. ans.pop();
  37. }
  38. break;
  39. }
  40. }
  41. }
  42. if(!ans.empty()) ans.pop();
  43. return false;
  44. }
  45.  
  46. int main(){
  47. cin>>n;
  48. cin>>t;
  49. cin>>m;
  50. string tmp,tmp2;
  51. for(int i = 0; i<m; i++){
  52. cin>>tmp;
  53. tmp2 = lower(tmp);
  54. words.insert(tmp2);
  55. mymap[tmp2] = tmp;
  56. mz = max(mz,(int)tmp.size());
  57. }
  58. reverse(t.begin(),t.end());
  59. sol(t);
  60. return 0;
  61. }
Success #stdin #stdout 0s 3476KB
stdin
30
ariksihsidlihcdnaehsetahgnisol
10
Kira
hates
is
he
losing
death
childish
L
and
Note
stdout
Kira is childish and he hates losing