Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. import java.util.Map;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.ArrayList;
  5.  
  6. public class SuffixTree{
  7. class Node{
  8. private final char currentValue;
  9. private Map<Character,Node> children;
  10. Node(){
  11. this.currentValue = '*';
  12. this.children = new HashMap<Character,Node>();
  13. }
  14. Node(char currentValue){
  15. this.currentValue = currentValue;
  16. this.children = new HashMap<Character,Node>();
  17. }
  18.  
  19. char getValue(){
  20. return this.currentValue;
  21. }
  22.  
  23. void addChild(Node c){
  24. this.children.put(c.getValue(),c);
  25. }
  26.  
  27. boolean hasChild(Node c){
  28. return this.children.containsKey(c.getValue());
  29. }
  30.  
  31. Node getChild(Node c){
  32. return this.children.get(c.getValue());
  33. }
  34.  
  35. public String toString(){
  36. char currentValue = this.getValue();
  37. StringBuffer keys = new StringBuffer();
  38. for(char key:this.children.keySet()){
  39. keys.append("Current key:"+key+"n");
  40. }
  41. return "Current value:"+currentValue+"n"+
  42. "Keys:"+keys.toString();
  43. }
  44. }
  45.  
  46. private Node root;
  47.  
  48. private void log(Object l){
  49. System.out.println(l);
  50. }
  51. /*
  52. * Helper method that initializes the suffix tree
  53. */
  54. private Node createSuffixTree(String source,Node root){
  55. for(int i=0;i<source.length();i++){
  56. Node parent = new Node(source.charAt(i));
  57. if(root.hasChild(parent)){
  58. parent = root.getChild(parent);
  59. }
  60. Node current = parent;
  61. for(int j=i+1;j<source.length();j++){
  62. Node temp = new Node(source.charAt(j));
  63. if(current.hasChild(temp)){
  64. temp = current.getChild(temp);
  65. }else{
  66. current.addChild(temp);
  67. }
  68. current = temp;
  69. }
  70. root.addChild(parent);
  71. }
  72. return root;
  73. }
  74.  
  75. /*
  76. Creates the suffix tree from the given string
  77. */
  78. public SuffixTree(String source){
  79. this.root = createSuffixTree(source,new Node());
  80. }
  81.  
  82. void printMap(Map<Character,Node> map){
  83. for(char c:map.keySet()){
  84. System.out.println("Current node has child"+c);
  85. }
  86. }
  87.  
  88. boolean search(String target){
  89. Map<Character,Node> rootChildren = this.root.children;
  90. for(char c:target.toCharArray()){
  91. if(rootChildren.containsKey(c)){
  92. rootChildren = rootChildren.get(c).children;
  93. }else{
  94. return false;
  95. }
  96. }
  97. return true;
  98. }
  99.  
  100. public static void main(String[] args){
  101. SuffixTree sTree = new SuffixTree("bananas");
  102. List<String> input = new ArrayList<String>(){{
  103. add("ba");
  104. add("ban");
  105. add("ana");
  106. add("anas");
  107. add("nan");
  108. add("anans");
  109. add("ananas");
  110. add("n");
  111. add("s");
  112. add("as");
  113. add("naab");
  114. add("baan");
  115. add("aan");
  116. }};
  117. for(String i:input){
  118. String exists = "exists";
  119. if(!sTree.search(i)){
  120. exists = "doesn't exist";
  121. }
  122. System.out.println("Input:"+i+" "+exists);
  123. }
  124. }
  125. }
  126.  
  127. public class StringUtil {
  128.  
  129. public static void main(String[] args){
  130. String input = "bananas";
  131. List<String> substrings = new ArrayList<String>(){{
  132. add("ba");
  133. add("ban");
  134. add("ana");
  135. add("anas");
  136. add("nan");
  137. add("anans");
  138. add("ananas");
  139. add("n");
  140. add("s");
  141. add("as");
  142. add("naab");
  143. add("baan");
  144. add("aan");
  145. }};
  146. if(StringUtil.containsAllSubstrings(input, substrings)) {
  147. System.out.println("Input: "" + input + "" contains all substrings.");
  148. } else {
  149. System.out.println("Input: "" + input + "" does not contain all substrings.");
  150. }
  151. }
  152.  
  153. public static Boolean containsAllSubstrings( String input, List<String> substrings) {
  154. for(String substring : substrings) {
  155. if(!input.contains(substring)) {
  156. return false;
  157. }
  158. }
  159. return true;
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement