1 / 10

Where’s the title?

Where’s the title?. You gotta search for it!. PotW Solution. String s =  new  Scanner(System. in ).next(); int [] prev =  new int [ s.length () * 2 + 2]; Arrays. fill ( prev , -1); int  c = s.length (), ans = 0; for  ( int i = 0; i < s.length (); i ++) {

mac
Download Presentation

Where’s the title?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Where’s the title? You gotta search for it!

  2. PotW Solution String s = new Scanner(System.in).next(); int[] prev = newint[s.length() * 2 + 2]; Arrays.fill(prev, -1); int c = s.length(), ans = 0; for (inti = 0; i < s.length(); i++) { if (s.charAt(i) == '(')     { if (prev[c] == -1) prev[c] = i; c++;     } else     { prev[c] = -1;         c--; if (prev[c] != -1) ans = Math.max(ans, i - prev[c] + 1);     } } System.out.println(ans);

  3. Graphs • Graph = collection of nodes and edges • Connected component = path from every node to every other node • Distance = # of edges from one node to another • Can be represented as adjacency list(e.g. array of ArrayLists) – list of adjacent nodes for each node =

  4. Search Algorithms • An algorithm to find an item with certain properties among a collection • Lowest/highest value, closest distance, etc. • Search trees – Tree with data values in nodes stored in such a way that they can be traversed in order in some systematic way

  5. Floodfill • Given a m by n grid of colored tiles, find connected regions of black tiles • Analogous to a graph where the nodes are tiles and edges are adjacencies between tiles • This was used in the November bronze/silver contests • Beauty Pageant problem is reduced to a connectivity problem (floodfill) + finding distances between connected regions • Solve floodfill using Depth First Search (DFS) • Recursively expand nodes • Keep expanding until you hit a dead end

  6. Depth First Search (DFS) boolean[] visited // initialized to false dfs(current) ifcurrent equals end // end found exit dfs mark current as visited forall neighbors of current ifneighbor is not visited dfs(neighbor) • Sometimes useful to incorporate an "end" node • Might also be convenient to give DFS a return value of true/false • Denotes whether end was found • Java's stack memory limit is very unforgiving • Replace recursion with a Stack if the number of nodes exceeds a few thousand

  7. Finding Distances • DFS doesn't quite cut it if you're looking for distances • DFS explores the nodes in a head on, haphazard fashion • Opt for Breadth First Search (BFS) • Start from nodes of distance 0, expand to distance 1, etc., basically exploring in order of distance • Only works if all edges have length 1 • Use a queue - nodes closer are explored first • Note that this simply replaces the stack used in DFS with a queue • Not quite as efficient or easy to code as DFS, so only use BFS if distances are needed

  8. Breadth First Search (BFS) boolean[] visited // initialized to false int[] dist bfs(start) QueueQ visit start, set distance to zero push start into Q whileQ is not empty pop first element of Q forall neighbors of the first element ifneighbor not visited visit neighbor, set distance of neighbor add neighbor to queue • Save distances in a dist array • Set distance of neighbor to (current distance + 1) • Java has a Queue interface, but underneath you still need to use LinkedList, which implements Queue • Can do the same for Stack in DFS

  9. PotW – Friendship Trees • This problem has nothing to do with cows, but for some reason cows are nodes, and friendships are edges. Check if the graph is a tree. • Each edge is undirected, since friendships are mutual (hopefully) • A tree is a graph such that all nodes are connected and there is only one unique way to get between each two nodes

  10. Friendship Trees (cont.) • The first line of the input contains the number of nodes and then the number of edges • Each successive line contains an edge, and has two indices, where indices are numbered starting from one • The output should be either "YES" or "NO" • Sample Input:        Sample Output:3 2                           YES1 22 3 2 1 • 3

More Related