Hashmap类:
对于Hashmap的用法,这里只是一道小例题。Hashmap是非常强大的类,而且应用广泛。具体查java API 和其他博客。
Gdufe_2018_Summer III(J)
Babelfish
You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.
Input
Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.
Output
Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".
Sample Input
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay
atcay
ittenkay
oopslay
Sample Output
cat
eh
loops
Hint
Huge input and output,scanf and printf are recommended.
题解:查找右边的单词是否左边有对应,有的话,输出该单词,没有输出eh。
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String,String> map = new HashMap<String,String>();
while(scan.hasNext()){
String str = "";
String a = "";
while(!(a=scan.nextLine()).equals(str)){
String[] st = a.split(" ");
map.put(st[1], st[0]);
}
while(scan.hasNext()){
String str2 = scan.next();
if(map.get(str2)!=null)
System.out.println(map.get(str2));
else
System.out.println("eh");
}
}
}
}
TreeSet类:
TreeSet是一个集合类,它具有集合的特性。并且集合内的元素自动以字典序排序好。
一道GDUFE OJ1106
Alien And Password
Time Limit: 2000/1000ms (Java/Others)
Problem Description:
Alien Fred wants to destroy the Earth, but he forgot the password that activates the planet destroyer.
You are given a string S. Fred remembers that the correct password can be obtained from S by erasing exactly one character.
Write a program to calculate the number of different passwords Fred needs to try.
0)
"A"
Answer: 1
In this case, the only password Fred needs to try is an empty string.
1)
"ABA"
Answer: 3
The following three passwords are possible in this case: "BA", "AA", "AB".
2)
"AABACCCCABAA"
Answer: 7
3)
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
Answer: 1
Regardless of which character we erase, we will always obtain the same string. Thus there is only one possible password: the string that consists of 49 'Z's.
Input:
The input contains multiple cases.Each case contains a string(length less than 100).
Output:
For each case,output the answer of the problem.
Sample Input:
A
ABA
AABACCCCABAA
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
Sample Output:
1
3
7
1
Source:
Topcoder
题解:求去除字符串中一个字符后,字符的种类数。
import java.util.Scanner;
import java.util.TreeSet;
public class Main {
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
while(scan.hasNext()){
TreeSet set = new TreeSet();
String str = scan.next();
for(int i=0;i<str.length();i++){
String str1 = "";
for(int j=0;j<str.length();j++){
if(i!=j)
str1 += str.charAt(j);
}
set.add(str1);
}
System.out.println(set.size());
}
}
}