1001-滑雪
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 52 Accepted Submission(s) : 40
Problem Description
Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子
一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。
1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9
一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。
Input
输入的第一行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。
Output
输出最长区域的长度。
Sample Input
5 5 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9
Sample Output
25
import java.util.Scanner;
public class Main{
private static int a[][],b[][],n,m;
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
while(input.hasNext()){
n=input.nextInt();
m=input.nextInt();
int max=0;
a=new int[n+3][m+3];
b=new int[n+3][m+3];
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
a[i][j]=input.nextInt();
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
b[i][j]=f(i,j);
if(b[i][j]>max)
max=b[i][j];
}
}
System.out.println(max);
}
}
private static int f(int i, int j) {
int max=0;
if(i<1||j<1||i>n||j>m)
return 0;
if(b[i][j]>0)
return b[i][j];
if(a[i-1][j]<a[i][j]){
int sum=f(i-1,j);
max=sum;
}
if(a[i+1][j]<a[i][j]){
int sum=f(i+1,j);
if(sum>max)
max=sum;
}
if(a[i][j-1]<a[i][j]){
int sum=f(i,j-1);
if(sum>max)
max=sum;
}
if(a[i][j+1]<a[i][j]){
int sum=f(i,j+1);
if(sum>max)
max=sum;
}
return max+1;
}
}
1002-Function Run Fun
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 49 Accepted Submission(s) : 31
Problem Description
We all love recursion! Don't we?
Consider a three-parameter recursive function w(a, b, c):
if a <= 0 or b <= 0 or c <= 0, then w(a, b, c) returns:
1
if a > 20 or b > 20 or c > 20, then w(a, b, c) returns:
w(20, 20, 20)
if a < b and b < c, then w(a, b, c) returns:
w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c)
otherwise it returns:
w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1)
This is an easy function to implement. The problem is, if implemented directly, for moderate values of a, b and c (for example, a = 15, b = 15, c = 15), the program takes hours to run because of the massive recursion.
Consider a three-parameter recursive function w(a, b, c):
if a <= 0 or b <= 0 or c <= 0, then w(a, b, c) returns:
1
if a > 20 or b > 20 or c > 20, then w(a, b, c) returns:
w(20, 20, 20)
if a < b and b < c, then w(a, b, c) returns:
w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c)
otherwise it returns:
w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1)
This is an easy function to implement. The problem is, if implemented directly, for moderate values of a, b and c (for example, a = 15, b = 15, c = 15), the program takes hours to run because of the massive recursion.
Input
The input for your program will be a series of integer triples, one per line, until the end-of-file flag of -1 -1 -1. Using the above technique, you are to calculate w(a, b, c) efficiently and print the result.
Output
Print the value for w(a,b,c) for each triple.
Sample Input
1 1 1 2 2 2 10 4 6 50 50 50 -1 7 18 -1 -1 -1
Sample Output
w(1, 1, 1) = 2 w(2, 2, 2) = 4 w(10, 4, 6) = 523 w(50, 50, 50) = 1048576 w(-1, 7, 18) = 1
Source
PKU
import java.util.Scanner;
public class Main {
private static int s[][][]=new int[22][22][22];
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (true) {
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
if (a == -1 && b == -1 && c == -1)
break;
System.out.println("w(" + a + ", " + b + ", " + c + ") = "+ w(a, b, c));
}
}
private static int w(int a, int b, int c) {
if (a <= 0 || b <= 0 || c <= 0)
return 1;
else if (a > 20 || b > 20 || c > 20)
return s[20][20][20] = w(20, 20, 20);
else if (s[a][b][c] > 0)
return s[a][b][c];
else if (a < b && b < c)
return s[a][b][c]=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
else
return s[a][b][c]=w(a-1,b,c)+w(a-1,b-1,c)+w(a-1,b,c-1)-w(a-1,b-1,c-1);
}
}
1003-What Are You Talking About
Time Limit : 10000/5000ms (Java/Other) Memory Limit : 102400/204800K (Java/Other)
Total Submission(s) : 13 Accepted Submission(s) : 8
Problem Description
Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book into English. Can you help him?
Input
The problem has only one test case, the test case consists of two parts, the dictionary part and the book part. The dictionary part starts with a single line contains a string "START", this string should be ignored, then some lines follow, each line contains two strings, the first one is a word in English, the second one is the corresponding word in Martian's language. A line with a single string "END" indicates the end of the directory part, and this string should be ignored. The book part starts with a single line contains a string "START", this string should be ignored, then an article written in Martian's language. You should translate the article into English with the dictionary. If you find the word in the dictionary you should translate it and write the new word into your translation, if you can't find the word in the dictionary you do not have to translate it, and just copy the old word to your translation. Space(' '), tab('\t'), enter('\n') and all the punctuation should not be translated. A line with a single string "END" indicates the end of the book part, and that's also the end of the input. All the words are in the lowercase, and each word will contain at most 10 characters, and each line will contain at most 3000 characters.
Output
In this problem, you have to output the translation of the history book.
Sample Input
START from fiwo hello difh mars riwosf earth fnnvk like fiiwj END START difh, i'm fiwo riwosf. i fiiwj fnnvk! END
Sample Output
hello, i'm from mars. i like earth!HintHuge input, scanf is recommended.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
// Scanner input=new Scanner(System.in);
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
try {
String s = bf.readLine();
HashMap<String, String> map = new HashMap<String, String>();
while (!(s = bf.readLine()).equals("END")) {
String a[] = s.split(" ");
map.put(a[1], a[0]);
}
s=bf.readLine();
while(!(s = bf.readLine()).equals("END")){
boolean ok=true;
int x=0;
for(int i=0;i<s.length();i++){
char e=s.charAt(i);
if(e>='a'&&e<='z'){
if(ok){
x=i;
ok=false;
}
}
else{
if(ok==false){
String r=s.substring(x, i);
//System.out.println("----"+r);
if(map.get(r)!=null){
System.out.print(map.get(r));
}
else
System.out.print(r);
}
System.out.print(e);
ok=true;
}
}
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
1004-T9
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 1 Accepted Submission(s) : 1
Problem Description
A while ago it was quite cumbersome to create a message for the Short Message Service (SMS) on a mobile phone. This was because you only have nine keys and the alphabet has more than nine letters, so most characters could only be entered by pressing one key several times. For example, if you wanted to type "hello" you had to press key 4 twice, key 3 twice, key 5 three times, again key 5 three times, and finally key 6 three times. This procedure is very tedious and keeps many people from using the Short Message Service.
This led manufacturers of mobile phones to try and find an easier way to enter text on a mobile phone. The solution they developed is called T9 text input. The "9" in the name means that you can enter almost arbitrary words with just nine keys and without pressing them more than once per character. The idea of the solution is that you simply start typing the keys without repetition, and the software uses a built-in dictionary to look for the "most probable" word matching the input. For example, to enter "hello" you simply press keys 4, 3, 5, 5, and 6 once. Of course, this could also be the input for the word "gdjjm", but since this is no sensible English word, it can safely be ignored. By ruling out all other "improbable" solutions and only taking proper English words into account, this method can speed up writing of short messages considerably. Of course, if the word is not in the dictionary (like a name) then it has to be typed in manually using key repetition again.
Figure 8: The Number-keys of a mobile phone.
More precisely, with every character typed, the phone will show the most probable combination of characters it has found up to that point. Let us assume that the phone knows about the words "idea" and "hello", with "idea" occurring more often. Pressing the keys 4, 3, 5, 5, and 6, one after the other, the phone offers you "i", "id", then switches to "hel", "hell", and finally shows "hello".
Write an implementation of the T9 text input which offers the most probable character combination after every keystroke. The probability of a character combination is defined to be the sum of the probabilities of all words in the dictionary that begin with this character combination. For example, if the dictionary contains three words "hell", "hello", and "hellfire", the probability of the character combination "hell" is the sum of the probabilities of these words. If some combinations have the same probability, your program is to select the first one in alphabetic order. The user should also be able to type the beginning of words. For example, if the word "hello" is in the dictionary, the user can also enter the word "he" by pressing the keys 4 and 3 even if this word is not listed in the dictionary.
This led manufacturers of mobile phones to try and find an easier way to enter text on a mobile phone. The solution they developed is called T9 text input. The "9" in the name means that you can enter almost arbitrary words with just nine keys and without pressing them more than once per character. The idea of the solution is that you simply start typing the keys without repetition, and the software uses a built-in dictionary to look for the "most probable" word matching the input. For example, to enter "hello" you simply press keys 4, 3, 5, 5, and 6 once. Of course, this could also be the input for the word "gdjjm", but since this is no sensible English word, it can safely be ignored. By ruling out all other "improbable" solutions and only taking proper English words into account, this method can speed up writing of short messages considerably. Of course, if the word is not in the dictionary (like a name) then it has to be typed in manually using key repetition again.
![](https://i-blog.csdnimg.cn/blog_migrate/97b69b60f293cd43ee8339a43b8ee22f.gif)
Figure 8: The Number-keys of a mobile phone.
More precisely, with every character typed, the phone will show the most probable combination of characters it has found up to that point. Let us assume that the phone knows about the words "idea" and "hello", with "idea" occurring more often. Pressing the keys 4, 3, 5, 5, and 6, one after the other, the phone offers you "i", "id", then switches to "hel", "hell", and finally shows "hello".
Write an implementation of the T9 text input which offers the most probable character combination after every keystroke. The probability of a character combination is defined to be the sum of the probabilities of all words in the dictionary that begin with this character combination. For example, if the dictionary contains three words "hell", "hello", and "hellfire", the probability of the character combination "hell" is the sum of the probabilities of these words. If some combinations have the same probability, your program is to select the first one in alphabetic order. The user should also be able to type the beginning of words. For example, if the word "hello" is in the dictionary, the user can also enter the word "he" by pressing the keys 4 and 3 even if this word is not listed in the dictionary.
Input
The first line contains the number of scenarios.
Each scenario begins with a line containing the number w of distinct words in the dictionary (0<=w<=1000). These words are given in the next w lines. (They are not guaranteed in ascending alphabetic order, although it's a dictionary.) Every line starts with the word which is a sequence of lowercase letters from the alphabet without whitespace, followed by a space and an integer p, 1<=p<=100, representing the probability of that word. No word will contain more than 100 letters.
Following the dictionary, there is a line containing a single integer m. Next follow m lines, each consisting of a sequence of at most 100 decimal digits 2-9, followed by a single 1 meaning "next word".
Each scenario begins with a line containing the number w of distinct words in the dictionary (0<=w<=1000). These words are given in the next w lines. (They are not guaranteed in ascending alphabetic order, although it's a dictionary.) Every line starts with the word which is a sequence of lowercase letters from the alphabet without whitespace, followed by a space and an integer p, 1<=p<=100, representing the probability of that word. No word will contain more than 100 letters.
Following the dictionary, there is a line containing a single integer m. Next follow m lines, each consisting of a sequence of at most 100 decimal digits 2-9, followed by a single 1 meaning "next word".
Output
The output for each scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1.
For every number sequence s of the scenario, print one line for every keystroke stored in s, except for the 1 at the end. In this line, print the most probable word prefix defined by the probabilities in the dictionary and the T9 selection rules explained above. Whenever none of the words in the dictionary match the given number sequence, print "MANUALLY" instead of a prefix.
Terminate the output for every number sequence with a blank line, and print an additional blank line at the end of every scenario.
For every number sequence s of the scenario, print one line for every keystroke stored in s, except for the 1 at the end. In this line, print the most probable word prefix defined by the probabilities in the dictionary and the T9 selection rules explained above. Whenever none of the words in the dictionary match the given number sequence, print "MANUALLY" instead of a prefix.
Terminate the output for every number sequence with a blank line, and print an additional blank line at the end of every scenario.
Sample Input
2 5 hell 3 hello 4 idea 8 next 8 super 3 2 435561 43321 7 another 5 contest 6 follow 3 give 13 integer 6 new 14 program 4 5 77647261 6391 4681 26684371 77771
Sample Output
Scenario #1: i id hel hell hello i id ide idea Scenario #2: p pr pro prog progr progra program n ne new g in int c co con cont anoth anothe another p pr MANUALLY MANUALLY
1005-The Suspects
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 40000/20000K (Java/Other)
Total Submission(s) : 23 Accepted Submission(s) : 12
Problem Description
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others.
In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP).
Once a member in a group is a suspect, all members in the group are suspects.
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.
In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP).
Once a member in a group is a suspect, all members in the group are suspects.
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.
Input
The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space.
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
Output
For each case, output the number of suspects in one line.
Sample Input
100 4 2 1 2 5 10 13 11 12 14 2 0 1 2 99 2 200 2 1 5 5 1 2 3 4 5 1 0 0 0
Sample Output
4 1 1
import java.util.Scanner;
public class Main{
private static int a[];
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
while(true){
int n=input.nextInt();
int m=input.nextInt();
if(n==0&&m==0)
break;
a=new int[n+2];
for(int i=1;i<n;i++){
a[i]=i;
}
for(int i=0;i<m;i++){
int x=input.nextInt();
int k=0;
if(x>0){
k=input.nextInt();
}
for(int j=1;j<x;j++){
int f=input.nextInt();
hebing(k,f);
}
}
int sum=1;
for(int i=1;i<n;i++){
if(find(0)==find(i))
sum++;
}
System.out.println(sum);
}
}
private static void hebing(int x, int k) {
int dx=find(x);
int dy=find(k);
if(dx!=dy){
a[dy]=dx;
}
}
private static int find(int x) {
while(x!=a[x])
x=a[x];
return x;
}
}
1006-钥匙计数之一
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 5 Accepted Submission(s) : 2
Problem Description
一把锁匙有N个槽,槽深为1,2,3,4。每锁匙至少有3个不同的深度且至少有1对相连的槽其深度之差为3。求这样的锁匙的总数。
Input
本题无输入
Output
对N>=2且N<=31,输出满足要求的锁匙的总数。
Sample Output
N=2: 0 N=3: 8 N=4: 64 N=5: 360 .. .. .. .. .. .. .. N=31: ... 注:根据Pku Judge Online 1351 Number of Locks或 Xi'an 2002 改编,在那里N<=16
public class Main{//深搜超时,打表;
static String a[]={"0","0","0",
"8"
,"64"
,"360"
,"1776"
,"8216"
,"36640"
,"159624"
,"684240"
,"2898296"
,"12164608"
,"50687208"
,"209961648"
,"865509848"
,"3553389280"
,"14538802248"
,"59313382032"
,"241378013240"
,"980200805824"
,"3973116354984"
,"16078778126448"
,"64978668500120"
,"262277950619296"
,"1057528710767880"
,"4260092054072400"
,"17147133531655928"
,"68968784226289024"
,"277229417298013800"
,"1113741009496217136"
,"4472142617535586136"
};
public static void main(String[] args) {
for(int i=2;i<=31;i++){
System.out.println("N="+i+": "+a[i]);
}
}
}
/*
public class Main{//深搜N>12就基本超时了,
static int n=0;
static long sum=0;
public static void main(String[] args) {
for(n=2;n<=31;n++){
sum=0;
String s="";
dfs(s);
System.out.println("N="+n+": "+sum);
}
}
private static void dfs(String s) {
if(s.length()==n){
int m=0;
if(s.indexOf("1")!=-1)
m++;
if(s.indexOf("2")!=-1)
m++;
if(s.indexOf("3")!=-1)
m++;
if(s.indexOf("4")!=-1)
m++;
if(m>=3&&(s.indexOf("14")!=-1||s.indexOf("41")!=-1)){
sum++;
}
return;
}
for(int i=1;i<=4;i++){
dfs(s+i);
}
}
}*/
1007-Square
Time Limit : 10000/5000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 31 Accepted Submission(s) : 14
Problem Description
Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square?
Input
The first line of input contains N, the number of test cases. Each test case begins with an integer 4 <= M <= 20, the number of sticks. M integers follow; each gives the length of a stick - an integer between 1 and 10,000.
Output
For each case, output a line containing "yes" if is is possible to form a square; otherwise output "no".
Sample Input
3 4 1 1 1 1 5 10 20 30 40 50 8 1 7 2 6 4 4 3 5
Sample Output
yes no yes
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
static Integer a[];
static int eve,n;
static boolean ok=false,vis[];
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int N = input.nextInt();
while (N-- > 0) {
ok=false;
n=input.nextInt();
a=new Integer[n];
vis=new boolean[n];
int sum=0;
for(int i=0;i<n;i++){
a[i]=input.nextInt();
sum+=a[i];
}
if(sum%4!=0){
System.out.println("no");
continue;
}
eve=sum>>2;
Arrays.sort(a,new Comparator<Integer>(){
@Override
public int compare(Integer arg0, Integer arg1) {
return arg1-arg0;
}
});
if(a[0]>eve){
System.out.println("no");
}
else{
dfs(0,0,0);
if(ok)
System.out.println("yes");
else
System.out.println("no");
}
}
}
private static void dfs(int b, int sum, int i) {
if(ok)
return;
if(sum==3){
ok=true;
return;
}
if(b==eve){
dfs(0,sum+1,sum+1);
return;
}
for(;i<n;i++){
if(!vis[i]&&b+a[i]<=eve){
vis[i]=true;
dfs(b+a[i],sum,i+1);
vis[i]=false;
}
}
}
}