Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define FOR(i,a,b) for (int i = (a); i < (b); i++)
  5. #define RFOR(i,b,a) for (int i = (b)-1; i >= (a); i--)
  6. #define ITER(it,a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++)
  7. #define FILL(a,value) memset(a, value, sizeof(a))
  8.  
  9. #define SZ(a) (int)a.size()
  10. #define ALL(a) a.begin(), a.end()
  11. #define PB push_back
  12. #define MP make_pair
  13.  
  14. typedef long long LL;
  15. typedef vector<int> VI;
  16. typedef pair<LL, LL> PII;
  17.  
  18. const double PI = acos(-1.0);
  19. const int INF = 1000 * 1000 * 1000 + 7;
  20. const LL LINF = INF * (LL) INF;
  21.  
  22. const int MAX = 110;
  23.  
  24. pair<string, string> A[MAX];
  25. int n;
  26.  
  27. map<pair<int, pair<string, string> >, LL > M;
  28.  
  29. string suf(int len, string& s)
  30. {
  31.     string res;
  32.     FOR (i, 0, len)
  33.     {
  34.         res += s[i + SZ(s) - len];
  35.     }
  36.  
  37.     return res;
  38. }
  39.  
  40. string pref(int len, string& s)
  41. {
  42.     string res;
  43.     FOR (i, 0, len)
  44.     {
  45.         res += s[i];
  46.     }
  47.  
  48.     return res;
  49. }
  50.  
  51. LL go(string a, string b, int st)
  52. {
  53.     if (a == b) return 0;
  54.     if (M.count(MP(st, MP(a, b)))) return M[MP(st, MP(a, b))];
  55.  
  56.     M[MP(st, MP(a, b))] = LINF;
  57.  
  58.     LL res = LINF;
  59.  
  60.     FOR (i, st, n)
  61.     {
  62.         int c = SZ(A[i].first);
  63.  
  64.         if (pref(SZ(a) - c, a) != pref(SZ(a) - c, b)) continue;
  65.  
  66.         LL left = go(suf(c, a), A[i].first, i+1);
  67.         if (left == LINF) continue;
  68.  
  69.         LL right = go(A[i].second, suf(c, b), i+1);
  70.  
  71.         if (right == LINF) continue;
  72.  
  73.         res = min(res, left + right + 1);
  74.     }
  75.  
  76.     M[MP(st, MP(a, b))] = res;
  77.  
  78.     return res;
  79. }
  80.  
  81. bool cmp(const pair<string, string>& a, const pair<string, string>& b)
  82. {
  83.     return SZ(a.first) > SZ(b.first);
  84. }
  85.  
  86. int main()
  87. {
  88.     //freopen("in.txt", "r", stdin);
  89.     //ios::sync_with_stdio(false); cin.tie(0);
  90.  
  91.     int test = 0;
  92.     while(true)
  93.     {
  94.         test++;
  95.         string a;
  96.         cin>>a;
  97.         if (a == ".") break;
  98.         string b;
  99.         cin>>b>>n;
  100.         FOR (i, 0, n)
  101.         {
  102.             cin>>A[i].first>>A[i].second;
  103.         }
  104.  
  105.         sort(A, A+n, cmp);
  106.  
  107.         M.clear();
  108.  
  109.         LL res = go(a, b, 0);
  110.  
  111.         cout<<"Case "<<test<<": ";
  112.         if (res == LINF) cout<<"No solution\n";
  113.         else cout<<res<<"\n";
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement