fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <bits/stdc++.h>
  6. #include <set>
  7. #include <ext/pb_ds/assoc_container.hpp>
  8. #include <ext/pb_ds/tree_policy.hpp>
  9. #include <map>
  10. #define ll long long
  11. using namespace __gnu_pbds;
  12. using namespace std;
  13. template <class T>
  14. using ordered_set = tree<T , null_type , less<T> , rb_tree_tag , tree_order_statistics_node_update>;
  15. // less<T>/greater<T> = ascending/descending.
  16. // less_equal<>/greater_equal<> for ordered multiset
  17. // ordered_multiset note : s.find(), s.erase() don't work + s.upper_bound() and s.lower_bound() swap jobs;
  18. void fastIO(void) {
  19. ios_base::sync_with_stdio(false);
  20. cin.tie(NULL);
  21. cout.tie(NULL);
  22. }
  23. int main(){
  24. fastIO();
  25. ll n; cin>>n;
  26. bool match;
  27. string temp;
  28. ll temp2;
  29. map<string,set<string>> names;
  30. for (int i = 0; i<n; i++){
  31. cin>>temp>>temp2;
  32. for (int j = 0; j<temp2; j++) {
  33. string temp3; cin>>temp3;
  34. names[temp].insert(temp3);
  35. }
  36. }
  37. for (auto i = names.begin(); i!=names.end(); i++) {
  38. for (auto it2 = i->second.begin(); it2!=i->second.end();){
  39. bool flag = 0;
  40. for (auto it3 = next(it2,1); it3!=i->second.end();) {
  41. bool flag2 = 0;
  42. bool match = 0;
  43. string str = *it2;
  44. string str2 = *it3;
  45. ll j = str.size()-1;
  46. ll k = str2.size()-1;
  47. while (j>=0 && k>=0) {
  48. if (str[j]==str2[k]) {
  49. if (j==0) {
  50. flag = 1;
  51. it2++;
  52. i->second.erase(prev(it2,1));
  53. match = 1;
  54. break;
  55. }
  56. else if (k==0) {
  57. flag2 = 1;
  58. it3++;
  59. i->second.erase(prev(it3,1));
  60. match = 1;
  61. break;
  62. }
  63. j--; k--;
  64. }
  65. else {
  66. break;
  67. }
  68. }
  69. if (!flag2) {
  70. it3++;
  71. }
  72. }
  73. if (!flag) {
  74. it2++;
  75. }
  76. }
  77. }
  78. cout<<names.size()<<'\n';
  79. for (auto i = names.begin(); i!=names.end(); i++) {
  80. cout<<i->first<<' '<<i->second.size()<<' ';
  81. for (auto it2 = i->second.begin(); it2!=i->second.end(); it2++){
  82. cout<<*it2<<' ';
  83. }
  84. cout<<'\n';
  85. }
  86. }
  87.  
Success #stdin #stdout 0.01s 5320KB
stdin
4
ivan 3 123 123 456
ivan 2 456 456
ivan 8 789 3 23 6 56 9 89 2
dasha 2 23 789
stdout
2
dasha 2 23 789 
ivan 4 123 2 456 789