Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <map>
  6. #include <queue>
  7.  
  8. using namespace std;
  9.  
  10. struct TRIE {
  11. map<char, TRIE> arcs;
  12.  
  13. void add(char w[]) {
  14. if(w[0] != '\0') {
  15. arcs[w[0]].add(w+1);
  16. }
  17. }
  18.  
  19. int check(char* w, int i = 0) {
  20. if(w[0] != '\0' && arcs.size() <= 2) {
  21. map<char, TRIE>::iterator it = arcs.find(w[0]);
  22. if (it != arcs.end())
  23. return arcs[w[0]].check(w+1, i+1);
  24. else return i;
  25. } else return i;
  26. }
  27. };
  28.  
  29. int main() {
  30. const int MARGIN_OF_SIMILARITY = 3;
  31.  
  32. int amount, maxC;
  33. cout << "Enter amount of files: ";
  34. cin >> amount;
  35. cout << "Enter max characters: ";
  36. cin >> maxC;
  37.  
  38. int fileStartC = maxC/3, fileChangeC = maxC/3, fileEndC = maxC/3;
  39.  
  40. TRIE start;
  41. priority_queue<string> names;
  42.  
  43. for(int i = 0; i < amount; i++) {
  44. cout << "Enter " << i << ": ";
  45. string s;
  46. cin >> s;
  47.  
  48. string r = s;
  49. reverse(r.begin(), r.end());//O(s.end() - s.begin())
  50.  
  51. int similarStart = start.check((char *) s.data());//O(n)
  52. int similarEnd = start.check((char *) r.data());//O(n)
  53.  
  54. string print;
  55. //if(similarStart < MARGIN_OF_SIMILARITY && similarEnd < MARGIN_OF_SIMILARITY) {
  56. start.add((char *) s.data());//O(n)
  57. start.add((char *) r.data());//O(n)
  58. //}
  59.  
  60. names.push(s);
  61. }
  62.  
  63. while(!names.empty()) {
  64. string n = names.top();
  65. cout << endl << "for " << n << endl;
  66.  
  67. int similarStart = start.check((char *) n.data());//O(n)
  68. string t = n;
  69. reverse(t.begin(), t.end());
  70. cout << "reversed " << t << endl;
  71. int similarEnd = start.check((char *) t.data());//O(n)
  72.  
  73. cout << "similar: start " << similarStart << " end " << similarEnd << endl;
  74.  
  75. string print;
  76. if(similarStart < MARGIN_OF_SIMILARITY && similarEnd < MARGIN_OF_SIMILARITY) {
  77. if(n.length() > maxC) {
  78. cout << "length " << n.length() << endl;
  79. print = n.substr(0, maxC/2) + ".." + n.substr(n.length()-maxC/2, n.length());
  80. } else {
  81. print = n;
  82. }
  83. } else {
  84. int shouldStart = similarStart > fileStartC? fileStartC:similarStart;
  85. int shouldEnd = similarEnd > fileEndC? fileEndC:similarEnd;
  86. cout << "shouldStart " << shouldStart << " similarStart " << similarStart << " similarEnd " << n.length()-similarEnd
  87. << " shouldEnd " << n.length()-shouldEnd << endl;
  88. print = n.substr(0, shouldStart) + ".."
  89. + n.substr(similarStart, (n.length()-similarEnd)-similarStart) + ".."
  90. + n.substr(n.length()-shouldEnd, n.length());
  91. }
  92.  
  93. cout << print << endl;
  94. names.pop();
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement