fork download
  1. vector<int>dp(52,false);
  2. vector<string>ret;
  3. set<string>st;
  4. bool ok[51];
  5. vector<int>words[52];
  6. string S;
  7.  
  8. void solve(int pos, string line){
  9. if(pos == -1){
  10. ret.push_back(line);
  11. return ;
  12. }
  13.  
  14. for(int i : words[pos]){
  15. string now;
  16.  
  17. for(int k=i+1; k<=pos; k++) now += S[k];
  18.  
  19. if(line.size()) now += " ";
  20. now += line;
  21.  
  22. solve(i,now);
  23. }
  24. }
  25.  
  26. vector<string> Solution::wordBreak(string A, vector<string> &B) {
  27.  
  28. for(string s : B) st.insert(s);
  29. int n = A.size();
  30. S = A;
  31.  
  32.  
  33. for(int i=0; i<A.size(); i++){
  34. if(i>0 && dp[i-1]==false) continue;
  35. string now;
  36. for(int j=i; j<A.size(); j++){
  37. now += A[j];
  38. if(now.size()>20) break;
  39. if(dp[j]) continue;
  40. if(st.find(now) != st.end()) dp[j] = true;
  41. }
  42. }
  43. if(dp[n-1] == false) return ret;
  44.  
  45. ok[n] = true;
  46.  
  47. for(int i=n-1; i>=0; i--){
  48.  
  49. string now;
  50.  
  51. for(int j=i; j<A.size(); j++){
  52. now += A[j];
  53. if(now.size()>20) break;
  54. if(ok[j+1] == false) continue;
  55. if(st.find(now) != st.end()) ok[i] = true;
  56. }
  57. }
  58.  
  59. for(int i=0; i<n; i++){
  60. if(i>0 && dp[i-1]==false) continue;
  61.  
  62. for(int j=i; j<n; j++){
  63.  
  64. if((j-i+1)>20) break;
  65. if(ok[j+1] == false) continue;
  66.  
  67. if(dp[j]){
  68. words[j].push_back(i-1);
  69. }
  70. }
  71. }
  72.  
  73.  
  74. solve(n-1,"");
  75. sort(ret.begin(), ret.end());
  76. return ret;
  77. }
  78.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:1: error: no template named 'vector'
vector<int>dp(52,false);
^
prog.cpp:2:1: error: no template named 'vector'
vector<string>ret;
^
prog.cpp:2:8: error: use of undeclared identifier 'string'
vector<string>ret;
       ^
prog.cpp:3:1: error: no template named 'set'
set<string>st;
^
prog.cpp:3:5: error: use of undeclared identifier 'string'
set<string>st;
    ^
prog.cpp:5:1: error: no template named 'vector'
vector<int>words[52];
^
prog.cpp:6:1: error: unknown type name 'string'
string S;
^
prog.cpp:8:21: error: unknown type name 'string'
void solve(int pos, string line){
                    ^
prog.cpp:15:9: error: unknown type name 'string'
        string now;
        ^
prog.cpp:26:1: error: no template named 'vector'
vector<string> Solution::wordBreak(string A, vector<string> &B) {
^
prog.cpp:26:8: error: use of undeclared identifier 'string'
vector<string> Solution::wordBreak(string A, vector<string> &B) {
       ^
prog.cpp:26:16: error: use of undeclared identifier 'Solution'
vector<string> Solution::wordBreak(string A, vector<string> &B) {
               ^
prog.cpp:26:36: error: unknown type name 'string'
vector<string> Solution::wordBreak(string A, vector<string> &B) {
                                   ^
prog.cpp:26:46: error: no template named 'vector'
vector<string> Solution::wordBreak(string A, vector<string> &B) {
                                             ^
prog.cpp:26:53: error: use of undeclared identifier 'string'
vector<string> Solution::wordBreak(string A, vector<string> &B) {
                                                    ^
15 errors generated.
stdout
Standard output is empty