Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.*;
  3. import java.lang.*;
  4. import java.io.*;
  5. import java.util.LinkedList;
  6.  
  7. class MaxFlow
  8. {
  9. public int V; //Number of vertices in graph
  10.  
  11. /* Returns true if there is a path from source 's' to sink
  12. 't' in residual graph. Also fills parent[] to store the
  13. path */
  14. boolean bfs(int rGraph[][], int s, int t, int parent[])
  15. {
  16. // Create a visited array and mark all vertices as not
  17. // visited
  18. boolean visited[] = new boolean[V];
  19. for(int i=0; i<V; ++i)
  20. visited[i]=false;
  21.  
  22. // Create a queue, enqueue source vertex and mark
  23. // source vertex as visited
  24. LinkedList<Integer> queue = new LinkedList<Integer>();
  25. queue.add(s);
  26. visited[s] = true;
  27. parent[s]=-1;
  28.  
  29. // Standard BFS Loop
  30. while (queue.size()!=0)
  31. {
  32. int u = queue.poll();
  33.  
  34. for (int v=0; v<V; v++)
  35. {
  36. if (visited[v]==false && rGraph[u][v] > 0)
  37. {
  38. queue.add(v);
  39. parent[v] = u;
  40. visited[v] = true;
  41. }
  42. }
  43. }
  44.  
  45. // If we reached sink in BFS starting from source, then
  46. // return true, else false
  47. return (visited[t] == true);
  48. }
  49.  
  50. // Returns tne maximum flow from s to t in the given graph
  51. int fordFulkerson(int graph[][], int s, int t)
  52. {
  53. int u, v;
  54.  
  55. // Create a residual graph and fill the residual graph
  56. // with given capacities in the original graph as
  57. // residual capacities in residual graph
  58.  
  59. // Residual graph where rGraph[i][j] indicates
  60. // residual capacity of edge from i to j (if there
  61. // is an edge. If rGraph[i][j] is 0, then there is
  62. // not)
  63. int rGraph[][] = new int[V][V];
  64.  
  65. for (u = 0; u < V; u++)
  66. for (v = 0; v < V; v++)
  67. rGraph[u][v] = graph[u][v];
  68.  
  69. // This array is filled by BFS and to store path
  70. int parent[] = new int[V];
  71.  
  72. int max_flow = 0; // There is no flow initially
  73.  
  74. // Augment the flow while tere is path from source
  75. // to sink
  76. while (bfs(rGraph, s, t, parent))
  77. {
  78. // Find minimum residual capacity of the edhes
  79. // along the path filled by BFS. Or we can say
  80. // find the maximum flow through the path found.
  81. int path_flow = Integer.MAX_VALUE;
  82. for (v=t; v!=s; v=parent[v])
  83. {
  84. u = parent[v];
  85. path_flow = Math.min(path_flow, rGraph[u][v]);
  86. }
  87.  
  88. // update residual capacities of the edges and
  89. // reverse edges along the path
  90. for (v=t; v != s; v=parent[v])
  91. {
  92. u = parent[v];
  93. rGraph[u][v] -= path_flow;
  94. rGraph[v][u] += path_flow;
  95. }
  96.  
  97. // Add path flow to overall flow
  98. max_flow += path_flow;
  99. }
  100.  
  101. // Return the overall flow
  102. return max_flow;
  103. }
  104. }
  105. public class Main {
  106.  
  107. /**
  108. * @param args the command line arguments
  109. */
  110. public static void main(String[] args) {
  111. Scanner in = new Scanner(System.in);
  112. String[] cubes = in.nextLine().split(" ");
  113. String[] words = in.nextLine().split(" ");
  114.  
  115. int counter = 0;
  116. for (String word : words){
  117.  
  118. int[][] mat = new int[cubes.length + word.length() + 2][cubes.length + word.length() + 2];
  119. //System.out.println("N" + (cubes.length + word.length() + 2));
  120. //System.out.println("M" + (cubes.length + word.length() + 2));
  121. for (int i = 1; i <= cubes.length; i++){
  122. mat[0][i] = 1;
  123. }
  124.  
  125. char[] characters = word.toCharArray();
  126. for (int i = 1; i <= cubes.length; i++){
  127. for (int j = 0; j < characters.length; j++){
  128. if (cubes[i - 1].indexOf(characters[j]) >= 0){ // contains
  129. mat[i][cubes.length + j + 1] = 1;
  130. }
  131. }
  132.  
  133. }
  134.  
  135. for (int i = cubes.length + 1; i <= cubes.length + word.length(); i++){
  136. mat[i][cubes.length + word.length() + 1] = 1;
  137. }
  138. MaxFlow mf = new MaxFlow();
  139. mf.V = cubes.length + word.length() + 2;
  140. if (word.length() == mf.fordFulkerson(mat, 0, cubes.length + word.length() + 1)){
  141. System.out.println(counter);
  142. }
  143. counter++;
  144. }
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement