Guest User

Untitled

a guest
Mar 13th, 2016
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.91 KB | None | 0 0
  1. /*  Given a list of strings L and a string S, determine
  2.     if S can be constructed by concatenating elements from L.
  3.     Each element can be used multiple times.                */
  4.  
  5. #include <iostream>
  6. #include <cstdio>
  7. #include <string>
  8. #include <climits>
  9. #include <algorithm>
  10. #include <cmath>
  11. #include <vector>
  12.  
  13. using namespace std;
  14.  
  15. vector<string> L;
  16. string S;
  17.  
  18. void getL();
  19. void getS();
  20. bool ifStringMatchS(string test, string S, int cutBegin);
  21. void solve(vector<string> L, string S);
  22.  
  23. int main() {
  24.     getL();
  25.     getS();
  26.     solve(L, S);
  27.     return 0;
  28. }
  29.  
  30. void getL() {
  31.     int amountOfStrings;
  32.     string temp_String;
  33.  
  34.     cout << "Type amount of strings in set L:" << endl;
  35.     cin >> amountOfStrings;
  36.  
  37.     cout << "Type " << amountOfStrings <<
  38.         " strings followed by enter:" << endl;
  39.     for (int i = 0; i < amountOfStrings; i++) {
  40.         cin >> temp_String;
  41.         L.push_back(temp_String);
  42.     }
  43. }
  44.  
  45. void getS() {
  46.     cout << "Type string S:" << endl;
  47.     cin >> S;
  48. }
  49.  
  50. bool ifStringMatchS(string test, string S, int cutBegin) {
  51.     string cuttedS = S.substr(cutBegin, test.size());
  52.     return !test.compare(cuttedS);
  53. }
  54.  
  55. void solve(vector<string> L, string S) {
  56.     vector<int> ifPossibleArray(S.size(), 0);
  57.     string cuttedS;
  58.     int L_ending;
  59.  
  60.     // go through ifPossibleArray to check if we are able to
  61.     // create beginning of first chars of S string
  62.     for (int i = 0; i < S.size(); i++) {
  63.  
  64.         // go through L array if we are able to match its
  65.         // elements with beginning of S string without first
  66.         // i chars
  67.         for (int j = 0; j < L.size(); j++) {
  68.  
  69.             // where L[j] ends on ifPossibleArray with cutted
  70.             // first i chars
  71.             L_ending = i + L[j].size() - 1;
  72.  
  73.             // do not check if we are not able to get there
  74.             if (i == 0 || ifPossibleArray[i - 1]) {
  75.                 // do not check if too long L element nor already checked
  76.                 if ((L_ending < S.size()) && ifPossibleArray[L_ending] == 0) {
  77.                     // do not update ifPossibleArray if string comaprision fails  
  78.                     if (ifStringMatchS(L[j], S, i)) {
  79.                         ifPossibleArray[L_ending] = j + 1;
  80.                     }
  81.                 }
  82.             }
  83.         }
  84.     }
  85.  
  86.     // print an example of sulution
  87.     if (ifPossibleArray[S.size() - 1]) {
  88.         cout << "solution exists:" << endl;
  89.         vector<string> printSolution;
  90.         int i = S.size() - 1;
  91.  
  92.         do {
  93.             printSolution.push_back(L[ifPossibleArray[i] - 1]);
  94.             i -= L[ifPossibleArray[i] - 1].size();
  95.         } while (i != -1);
  96.  
  97.         cout << S << " = ";
  98.         for (i = printSolution.size() - 1; i > 0; i--) {
  99.             cout << printSolution[i] << " + ";
  100.         }
  101.         cout << printSolution[i];
  102.     }
  103.     else {
  104.         cout << "solution doesn't exist";
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment