1.
/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest,
WS10/11 Problem:
* 11869 SOAP Response
* Link:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=226&page=show_problem&problem=2980
*
* @author Felix Dietrich
* @version 1.0, 24.10.2010
*
* Method : Object oriented
* Status : Accepted
* Runtime:
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main
{
public static class Tag
{
public Map<String,String>
properties = new HashMap<String,String>();
public List<Tag> childs =
new ArrayList<Main.Tag>();
public boolean open = true;
public String name;
public Tag parent;
public Tag(Tag p)
{
open = true;
parent = p;
}
public void setname(String s)
{
name = s;
}
public void addprop(String[] s)
{
properties.put(s[0], s[1]);
}
public void addc(Tag c)
{
childs.add(c);
}
public void close()
{
this.open =
false;
}
}
private static String[] p;
private static String[] parseprop(String string)
{
p = string.split("=");
p[1] =
p[1].substring(1,p[1].length()-1);
return p;
}
public static Tag parse(BufferedReader reader, int
lines) throws IOException
{
Tag res = null;
Tag currentTag = res;
Tag t;
String[] props;
for(int i=0; i<lines; i++)
{
String l =
reader.readLine();
if(res != null
&& currentTag == null)
return res;
if(l.length()
< 2)
{
continue;
}
if(l.substring(0,2).equals("</"))
{
currentTag.close();
currentTag = currentTag.parent;
}
else
{
t = new Tag(currentTag);
if(currentTag != null)
currentTag.addc(t);
currentTag = t;
if(res == null)
res = currentTag;
l = l.substring(1,l.length()-1);
props = l.split(" ");
currentTag.setname(props[0]);
for(int p=1; p<props.length; p++)
currentTag.addprop(parseprop(props[p]));
}
}
return res;
}
public static void main(String... s) throws
IOException
{
//Scanner sc = new
Scanner(System.in);
BufferedReader reader = new
BufferedReader(new InputStreamReader(System.in));
StringBuilder sb;
int lines;
Tag t;
Tag xx;
int queries;
String n,prop;
int tc;
int tcs =
Integer.parseInt(reader.readLine());
for (int c = 0; c < tcs; c++)
{
sb = new
StringBuilder(String.format("Case %d:\n", c+1));
lines =
Integer.parseInt(reader.readLine());
t =
parse(reader, lines);
xx = t;
queries =
Integer.parseInt(reader.readLine());
for(int q=0;
q<queries;q++)
{
t = xx;
String x = reader.readLine();
String[] tree = x.split("\\.");
for(tc=1;tc<tree.length-1;tc++)
{
t = findchild(t,tree[tc]);
if(t == null)
{
sb.append("Undefined\n");
break;
}
}
if(t == null)
{
continue;
}
n = tree[tree.length-1].split("\\[")[0];
prop = tree[tree.length-1].split("\\[")[1];
prop = prop.substring(1,prop.length()-2);
if(tree.length > 1)
t = findchild(t, n);
if(t == null || !t.properties.containsKey(prop))
{
sb.append("Undefined\n");
continue;
}
sb.append(t.properties.get(prop) + "\n");
}
System.out.print(sb.toString());
}
}
private static int kk;
private static Tag findchild(Tag t, String n)
{
for(kk=0; kk<t.childs.size();
kk++)
{
if(t.childs.get(kk).name.equals(n))
{
return t.childs.get(kk);
}
}
return null;
}
}
2.
/*
* ACM Contest training
* Problem: 11869 - SOAP Response
* Link:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=226&problem=2980
*
* @author Christoph Goettschkes
* @version 1.0, 10/27/2010
*
* Method : Ad-Hoc
* Status : Accepted
* Runtime: 0.976
*/
import java.io.*;
import java.util.*;
class Main
{
public static void main(String[] args) throws
Exception
{
BufferedReader reader = new
BufferedReader(new InputStreamReader(System.in));
String line =
reader.readLine().trim();
int testCases =
Integer.parseInt(line);
for (int i = 1; i <=
testCases; i++) {
int inputLines
= Integer.parseInt(reader.readLine().trim());
Node main =
new Node("ROOT", null);
parseInput(main, reader, inputLines);
System.out.println("Case " + i + ":");
int querys =
Integer.parseInt(reader.readLine().trim());
for (int j =
0; j < querys; j++) {
String wayLine = reader.readLine();
String[] way = wayLine.split("\\.");
Node current = main;
for (int k = 0; k < way.length - 1; k++) {
current =
current.getChild(way[k]);
if (current == null)
break;
}
if (current == null)
System.out.println("Undefined");
else {
String[] last = way[way.length -
1].split("\\[");
current =
current.getChild(last[0]);
if (current == null)
System.out.println("Undefined");
else {
String attr =
current.getAttribute(last[1].substring(1, last[1].length() - 2));
if (attr ==
null)
System.out.println("Undefined");
else
System.out.println(attr);
}
}
}
}
}
public static void parseInput(Node root,
BufferedReader reader, int lines) throws Exception
{
Node currentNode = root;
for (int lineNumber = 0;
lineNumber < lines; lineNumber++) {
String cur =
reader.readLine().trim();
if
(cur.charAt(1) == '/') {
currentNode = currentNode.getParentNode();
} else {
cur = cur.substring(1, cur.length() - 1);
StringTokenizer tokenizer = new StringTokenizer(cur);
Node child = new Node(tokenizer.nextToken(),
currentNode);
currentNode.setChild(child.getTag(), child);
parseAttrs(child, tokenizer);
currentNode = child;
}
}
}
public static void parseAttrs(Node node,
StringTokenizer tokenizer) {
while (tokenizer.hasMoreTokens())
{
String[] attr
= tokenizer.nextToken().split("=");
node.setAttribute(attr[0], attr[1].substring(1, attr[1].length() - 1));
}
}
}
class Node
{
private final String itsTag;
private final Map<String, Node> childs = new
HashMap<String, Node>();
private final Map<String, String> atts = new
HashMap<String, String>();
private final Node parentNode;
public Node(String tag, Node parent)
{
itsTag = tag;
parentNode = parent;
}
public Node getParentNode() {
return parentNode;
}
public String getTag() {
return itsTag;
}
public void setAttribute(String key, String value) {
atts.put(key, value);
}
public String getAttribute(String key) {
return atts.get(key);
}
public void setChild(String tag, Node node) {
childs.put(tag, node);
}
public Node getChild(String tag) {
return childs.get(tag);
}
@Override
public String toString() {
return itsTag + ": " +
atts.toString() + "\nChilds: " + childs;
}
}
3.
/**
* FWP, Ausgewählte Probleme aus dem ACM
Programming Contest, SS10
* Problem: 11869 - SOAP Response
* Link:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=226&page=show_problem&problem=2980
*
* Method : ad hoc
* Status : Accepted
* Runtime: 0.640
*
* @author Evgeni Pavlidis
* @version 1.0, 10/25/2010
*/
import java.io.*;
import java.util.*;
class Main
{
public static void main(String...args) throws
IOException
{
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
int testCases =
Integer.parseInt(br.readLine());
String input, path, name,
value;
String[] inputs;
int n,q;
Map<String, String>
map = new HashMap<String, String>();
for(int tc = 1; tc <=
testCases; tc++)
{
map.clear();
path
= "";
n =
Integer.parseInt(br.readLine());
for(int i = 0; i < n; i++)
{
input = br.readLine();
inputs = input.substring(1, input.length()-1).split(" ");
if(inputs[0].charAt(0) == '/')
path = path.substring(0, path.lastIndexOf('.')); //
go back
else
path += "." +
inputs[0];
// augment path
// iterate attributes and put them into map
for(int j = 1; j < inputs.length; j++)
{
name = inputs[j].substring(0, inputs[j].indexOf("="));
value = inputs[j].substring(inputs[j].indexOf("\"")+1,
inputs[j].length()-1);
map.put(path.substring(1) + "[\"" + name + "\"]", value);
}
}
System.out.println("Case " + tc + ":");
q =
Integer.parseInt(br.readLine());
for(int i = 0; i < q; i++)
{
input = br.readLine();
if(map.containsKey(input))
System.out.println(map.get(input));
else
System.out.println("Undefined");
}
}
}
}