fork download
  1. #include<bits/stdc++.h>
  2. #define deb(x) cout<<#x<<" "<<x<<endl;
  3. #define print(arr,n) for(int i=0;i<n;++i) { cout<<arr[i]<<" "; } cout<<endl;
  4. #define ub(x,el) upper_bound(x.begin(),x.end(),el) - x.begin()
  5. #define lb(x,el) lower_bound(x.begin(),x.end(),el) - x.begin();
  6. #define bs(x,el) binary_search(x.begin(),x.end(),el)
  7. #define MOD 1000000007
  8. #define finish return 0
  9. using namespace std;
  10. typedef long long int ll;
  11. ll mod(ll x) { return x%MOD; }
  12. ll power(ll x,ll y, ll p) { ll res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res*x) % p; y = y>>1; x = (x*x) % p; } return res; }
  13. ll mod(ll num, ll den){ return mod(mod(num) * power(den,MOD-2,MOD)); }
  14. bool compare(string &a, string &b)
  15. {
  16. return a.size()<b.size();
  17. }
  18. int main()
  19. {
  20. ios_base::sync_with_stdio(false);
  21. cin.tie(NULL);
  22. ll t;
  23. cin>>t;
  24. ll co = 1;
  25. while(t--)
  26. {
  27. ll n;
  28. cin>>n;
  29. vector<string> arr(n);
  30. for(ll i=0;i<n;++i) cin>>arr[i];
  31. vector< vector<string> > A(2, vector<string>(n));
  32. for(ll i=0;i<n;++i)
  33. {
  34. ll left = 0;
  35. while(left < arr[i].size())
  36. {
  37. if(arr[i][left] == '*') break;
  38. A[0][i] += arr[i][left];
  39. ++left;
  40. }
  41. ll right = arr[i].size()-1;
  42. while(right >= 0)
  43. {
  44. if(arr[i][right] == '*') break;
  45. A[1][i] += arr[i][right];
  46. --right;
  47. }
  48. reverse(A[1][i].begin(),A[1][i].end());
  49. }
  50. string head,tail;
  51.  
  52. string ans = "*";
  53. sort(A[0].begin(),A[0].end(),compare);
  54. string ref = A[0][n-1];
  55. bool ok = true;
  56. for(ll i=0;i<n-1;++i)
  57. {
  58. string &temp = A[0][i];
  59. string temp2 = ref.substr(0,temp.size());
  60. if(temp != temp2)
  61. {
  62. ok = false;
  63. break;
  64. }
  65. }
  66. cout<<"Case #"<<co<<": ";
  67. ++co;
  68. if(ok)
  69. {
  70. head = ref;
  71. sort(A[1].begin(),A[1].end(),compare);
  72. ref = A[1][n-1];
  73. for(ll i=0;i<n-1;++i)
  74. {
  75. string &temp = A[1][i];
  76. string temp2 = ref.substr(ref.size()-temp.size());
  77. if(temp != temp2)
  78. {
  79. ok = false;
  80. break;
  81. }
  82. }
  83. tail = ref;
  84. }
  85. if(ok)
  86. {
  87. ans = head;
  88. for(ll i=0;i<n;++i)
  89. {
  90. ll left = 0;
  91. while(left < arr[i].size())
  92. {
  93. if(arr[i][left] == '*') break;
  94. ++left;
  95. }
  96. ll right = arr[i].size()-1;
  97. while(right >= 0)
  98. {
  99. if(arr[i][right] == '*') break;
  100. --right;
  101. }
  102. for(ll j=left+1;j<=right-1;++j) if(arr[i][j] != '*') ans+=arr[i][j];
  103. }
  104. ans+=tail;
  105.  
  106. }
  107. cout<<ans<<endl;
  108.  
  109. }
  110. }
Runtime error #stdin #stdout #stderr 0s 4328KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc