/*--------------------------------------------------------------*/
/* ACM ICPC 2007-2008 */
/* Northeastern European Regional Contest */
/* St Petersburg - Barnaul - Tashkent - Batumi, November 28, 200*/
/*--------------------------------------------------------------*/
/* Problem L. Language Recognition */
/*--------------------------------------------------------------*/
/* Solution */
/* */
/* Author Roman Elizarov */
/*--------------------------------------------------------------*/
/*
Solution for NEERC'2007 Problem L: Language Recognition
(C) Roman Elizarov
Note: this solution attempts to check correctness of the input
*/
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
new Main().go();
}
class Node {
boolean f;
final Node[] next = new Node[26];
int hash;
public boolean equals(Object obj) {
if (!(obj instanceof Node))
return false;
Node o = (Node)obj;
if (f != o.f)
return false;
for (int i = 0; i < 26; i++) {
if (next[i] == null ? o.next[i] != null : !next[i].equals(o.next[i]))
return false;
}
return true;
}
public int hashCode() {
if (hash != 0)
return hash;
int h = 1;
for (int i = 0; i < 26; i++) {
h *= 31;
if (next[i] != null)
h += next[i].hashCode();
}
if (f)
h ^= 958748917;
return hash = h;
}
}
void go() throws Exception {
// read input
Scanner in = new Scanner(System.in);
//in.useLocale(Locale.US);
int n = in.nextInt();
in.nextLine();
assert n >= 1 && n <= 5000;
String[] w = new String[n];
for (int i = 0; i < n; i++) {
w[i] = in.next();
in.nextLine();
assert w[i].length() >= 1 && w[i].length() <= 30;
}
//in.close();
// solve
Node root = new Node();
for (int i = 0; i < n; i++) {
Node cur = root;
for (int j = 0; j < w[i].length(); j++) {
char c = w[i].charAt(j);
assert c >= 'a' && c <= 'z';
Node next = cur.next[c - 'a'];
if (next == null)
cur.next[c - 'a'] = next = new Node();
cur = next;
}
assert !cur.f : "duplicate word";
cur.f = true;
}
HashSet<Node> nodes = new HashSet<Node>();
addAll(root, nodes);
// write output
PrintWriter out = new PrintWriter("System.out");
out.println(nodes.size());
}
private void addAll(Node cur, HashSet<Node> nodes) {
if (nodes.add(cur))
for (int i = 0; i < 26; i++)
if (cur.next[i] != null)
addAll(cur.next[i], nodes);
}
//----------------- just for validation ------------------
/**
* Strict scanner to veryfy 100% correspondence between input files and input file format specification.
* It is a drop-in replacement for {@link java.util.Scanner} that could be added to a soulution source
* (cut-and-paste) without breaking its ability to work with {@link java.util.Scanner}.
*/
public class XScanner {
private final BufferedReader in;
private String line = "";
private int pos;
private int lineNo;
private boolean localeset;
public XScanner(File source) throws FileNotFoundException {
in = new BufferedReader(new FileReader(source));
nextLine();
}
public void close() {
assert line == null : "Extra data at the end of file";
try {
in.close();
} catch (IOException e) {
throw new AssertionError("Failed to close with " + e);
}
}
public void nextLine() {
assert line != null : "EOF";
assert pos == line.length() : "Extra characters on line " + lineNo;
try {
line = in.readLine();
} catch (IOException e) {
throw new AssertionError("Failed to read line with " + e);
}
pos = 0;
lineNo++;
}
public String next() {
assert line != null : "EOF";
assert line.length() > 0 : "Empty line " + lineNo;
if (pos == 0)
assert line.charAt(0) > ' ' : "Line " + lineNo + " starts with whitespace";
else {
assert pos < line.length() : "Line " + lineNo + " is over";
assert line.charAt(pos) == ' ' : "Wrong whitespace on line " + lineNo;
pos++;
assert pos < line.length() : "Line " + lineNo + " is over";
assert line.charAt(0) > ' ' : "Line " + lineNo + " has double whitespace";
}
StringBuilder sb = new StringBuilder();
while (pos < line.length() && line.charAt(pos) > ' ')
sb.append(line.charAt(pos++));
return sb.toString();
}
public int nextInt() {
String s = next();
assert s.length() == 1 || s.charAt(0) != '0' : "Extra leading zero in number " + s + " on line " + lineNo;
assert s.charAt(0) != '+' : "Extra leading '+' in number " + s + " on line " + lineNo;
try {
return Integer.parseInt(s);
} catch (NumberFormatException e) {
throw new AssertionError("Malformed number " + s + " on line " + lineNo);
}
}
public double nextDouble() {
assert localeset : "Locale must be set with useLocale(Locale.US)";
String s = next();
assert s.length() == 1 || s.startsWith("0.") || s.charAt(0) != '0' : "Extra leading zero in number " + s + " on line " + lineNo;
assert s.charAt(0) != '+' : "Extra leading '+' in number " + s + " on line " + lineNo;
try {
return Double.parseDouble(s);
} catch (NumberFormatException e) {
throw new AssertionError("Malformed number " + s + " on line " + lineNo);
}
}
public void useLocale(Locale locale) {
assert locale == Locale.US;
localeset = true;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
poj 部分题目源代码(共77题) for acm ,一年所做的
3星 · 超过75%的资源 需积分: 10 31 下载量 134 浏览量
2008-12-15
10:31:14
上传
评论
收藏 98KB TGZ 举报
温馨提示
共722个文件
cc:516个
cpp:181个
java:13个
poj 部分题目源代码(共77题) for acm ,一年所做的
资源推荐
资源详情
资源评论
收起资源包目录
poj 部分题目源代码(共77题) for acm ,一年所做的 (722个子文件)
3997919_CE.c 3KB
3614618_CE.c 3KB
3614623_CE.c 3KB
3632258_CE.c 2KB
3632245_CE.c 2KB
3632253_CE.c 2KB
3554333_CE.c 2KB
4321371_CE.c 1KB
3954816_CE.c 590B
3954811_CE.c 588B
3954808_AC_0MS_316K.c 588B
3943920_AC_0MS_312K.c 103B
3843425_CE.cc 19KB
3847884_AC_204MS_584K.cc 6KB
3847725_AC_219MS_544K.cc 5KB
3847732_AC_219MS_544K.cc 5KB
3937534_WA.cc 4KB
3937548_WA.cc 4KB
3937537_WA.cc 4KB
3937554_WA.cc 4KB
3937479_AC_94MS_592K.cc 4KB
3937502_WA.cc 4KB
3937472_AC_110MS_592K.cc 4KB
4283205_AC_1032MS_8852K.cc 4KB
3838584_WA.cc 4KB
3847059_AC_235MS_552K.cc 4KB
3846987_AC_172MS_552K.cc 4KB
3838459_WA.cc 4KB
3838337_WA.cc 4KB
4002163_AC_282MS_24060K.cc 4KB
3838318_WA.cc 4KB
4338311_AC_1391MS_14700K.cc 4KB
3909230_AC_391MS_6320K.cc 4KB
3909213_AC_375MS_6320K.cc 4KB
3909228_AC_360MS_6320K.cc 4KB
4286649_AC_16MS_424K.cc 4KB
3909232_RE.cc 3KB
3909235_RE.cc 3KB
4286396_AC_0MS_424K.cc 3KB
4286280_WA.cc 3KB
3909059_WA.cc 3KB
4286310_AC_63MS_424K.cc 3KB
4286401_AC_32MS_424K.cc 3KB
4286315_AC_16MS_424K.cc 3KB
4002145_AC_297MS_24064K.cc 3KB
3909188_WA.cc 3KB
3909181_CE.cc 3KB
3909180_WA.cc 3KB
3909122_WA.cc 3KB
3909179_WA.cc 3KB
4001029_WA.cc 3KB
4001002_WA.cc 3KB
4000998_TLE.cc 3KB
3635847_AC_47MS_1344K.cc 3KB
3635845_AC_63MS_1344K.cc 3KB
3635787_AC_47MS_1344K.cc 3KB
3936518_WA.cc 3KB
3936419_WA.cc 3KB
3937337_WA.cc 3KB
3838520_CE.cc 3KB
3936378_WA.cc 3KB
3989660_TLE.cc 3KB
3908990_WA.cc 3KB
3908959_AC_391MS_6804K.cc 3KB
3908992_AC_344MS_6804K.cc 3KB
3908870_AC_375MS_6804K.cc 3KB
3908885_MLE.cc 3KB
3908269_MLE.cc 3KB
3908272_MLE.cc 3KB
3909002_WA.cc 3KB
3908997_AC_344MS_6804K.cc 3KB
3997808_TLE.cc 3KB
3908947_WA.cc 3KB
3908943_WA.cc 3KB
3908938_WA.cc 3KB
3908892_WA.cc 3KB
3908036_WA.cc 3KB
3908030_WA.cc 3KB
3908034_WA.cc 3KB
3908010_WA.cc 3KB
3997918_TLE.cc 3KB
3908230_WA.cc 3KB
3908223_WA.cc 3KB
3935188_AC_1547MS_13972K.cc 3KB
3989826_WA.cc 3KB
3989823_TLE.cc 3KB
3998963_AC_63MS_496K.cc 3KB
3997884_TLE.cc 3KB
3998966_AC_47MS_496K.cc 3KB
3997830_TLE.cc 3KB
3907985_WA.cc 3KB
3907984_WA.cc 3KB
3908950_WA.cc 3KB
3998979_AC_47MS_404K.cc 3KB
3908123_WA.cc 3KB
3936339_WA.cc 3KB
3908881_WA.cc 3KB
3908953_WA.cc 3KB
3908149_MLE.cc 3KB
3908154_WA.cc 3KB
共 722 条
- 1
- 2
- 3
- 4
- 5
- 6
- 8
资源评论
- xiangyan08182012-03-29不错 很适合学习 题目很多 答案很详细
- messagersy2014-06-25还不错,看看有助于学习
theorix
- 粉丝: 2
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功