哈爾濱工業大學(威海)第九屆ACM程序設計競賽

A.All Palindrome

鏈接:https://ac.nowcoder.com/acm/contest/624/A
來源:牛客網

Ramen likes palindrome.

A palindrome is a word, number, phrase, or other sequences of characters which reads the same backward as forward, such as madam or racecar or the number 10801.

An all palindrome is such a string that each substring of it is a palindrome. For example, the string aaa is an all palindrome while aba is not.

There is a string, and Ramen wants to make it an all palindrome string. He has a magic skill that he can remove any character of the string as much as he wants. But cast such a spell will cost him a lot of energy. As a lazy boy, he wants to save energy to play games, so can you tell him the minimum number of the necessary characters that he needs to remove?

輸入描述:
The input contains precisely two lines.

The first line is an integer n(1 <= n <= 100000), which indicates the length of the given string.

The second line is the given string S. It’s guaranteed that S contains only lower case Lattin letters, i.e., a-z.
輸出描述:
For each test case, output the minimum number of necessary removals.
示例1
輸入
複製
22
welcometotheupcofhitwh
輸出
複製
19
示例2
輸入
複製
20
itstheeasiestproblem
輸出
複製
16
示例3
輸入
複製
25
goandgrabyoursignupreward
輸出
複製
21

這裏的全迴文的意思是隻剩一個單一字符組成的串,那麼找出最小花費肯定找母串中哪個字符最多

import java.io.BufferedInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;
public class Main {
    public static Scanner in=new Scanner(new BufferedInputStream(System.in));
    public static void main(String[] args) throws IOException, ParseException{
        int T=in.nextInt();
        String s=in.next();
        int []num=new int[26];
        for(int i=0;i<T;i++)
        {
            int tmp=s.charAt(i)-'a';
            num[tmp]++;
        }
        int max=0;
        for(int i=0;i<26;i++)
        {
            max=Math.max(max, num[i]);
        }
        System.out.println(T-max);
    }
}

B.Clarifications

鏈接:https://ac.nowcoder.com/acm/contest/624/B
來源:牛客網

題目描述
The university programming contest of HIT@WH is going to start!

As the main problem setter, Ramen is going to reply to the requests for clarification during the contest.

The level of the contestants is varied so that they will ask different questions. Some questions are foolish, the most stupid one among them is: Why I got WA on problem X?. Some are critical that may indicate that there is something went wrong, and juries need to investigate.

To make different responses for each request is unnecessary and boring, so Ramen has his own rules to answer the contestant. Here are the rules of how he makes a response:

  • If the request is a statement, he will answer No Response.
  • If the request is a stupid question, he will answer 42.
  • If the question is not a stupid question and the question has been asked less or equal than five times, he will answer Read the problem statement carefully.
  • If the question is not a stupid question and the question has been asked more than five times, he will answer Juries are investigating. Thanks.

A statement ends with a period(.) while a question ends with a question mark(?).

The contest is running, but Ramen has some urgent work to do, so he is absent. You know Ramen’s rules, can you help him for a while?
輸入描述:
The input contains multiple lines.

The first line contains two integers n, q(1 <= n <= 10000, 1 <= q <= 100), indicates the number of requests and the number of stupid questions.

Each of the next q lines contains a distinct stupid question.

Then each of the next n lines contains a request for clarification.

It’s guaranteed that the all given strings will only contain Latin letters and spaces, and each line ends with a period or a question mark. The length of each of them will not exceed 100.
輸出描述:
Output exactly n lines. The i-th line represents the answer to the i-th request of clarifications.
示例1
輸入
複製
10 2
Why I got WA on problem A?
How to solve problem C?
The problems are really good.
Why I got WA on problem A?
Why I got CE?
Why I got CE?
How to solve problem C?
Why I got CE?
Why I got CE?
Why I got CE?
The constraint of E is correct?
Why I got CE?
輸出
複製
No Response.
42.
Read the problem statement carefully.
Read the problem statement carefully.
42.
Read the problem statement carefully.
Read the problem statement carefully.
Read the problem statement carefully.
Read the problem statement carefully.
Juries are investigating. Thanks.
備註:
Do not forget to output the dot(.) at the end of each answer.

What does 42 mean? It’s the answer to life, the universe, and everything.

You can still ask any question during the contest. It’s guaranteed that juries will never answer you 42, seriously.

模擬題

import java.io.BufferedInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Scanner;
import java.util.TreeSet;
public class Main {
    public static Scanner in=new Scanner(new BufferedInputStream(System.in));
    public static void main(String[] args) throws IOException, ParseException{
        TreeSet<String> set=new TreeSet<String>();
        HashMap <String,Integer> map=new HashMap<String,Integer>();
        int n=in.nextInt();
        int q=in.nextInt();
        int ind=0;int num[]=new int[n];
        for(int i=0;i<=q;i++)
        {
            String tmp=in.nextLine();
            set.add(tmp);
        }
        for(int i=0;i<n;i++)
        {
            String s=in.nextLine();
            if(set.contains(s))
                System.out.println("42.");
            else
            {
                if(s.contains("."))
                    System.out.println("No Response.");
                else if(s.contains("?"))
                {
                    if(map.containsKey(s))
                        num[map.get(s)]++;
                    else
                        map.put(s,ind++);
                    if(num[map.get(s)]>=5)
                        System.out.println("Juries are investigating. Thanks.");
                    else
                        System.out.println("Read the problem statement carefully.");
                }
            }
        }
    }
}

C.∞

鏈接:https://ac.nowcoder.com/acm/contest/624/C
來源:牛客網

∞, the infinity. It’s one of the most complex math symbols. How does it mean? The mathematicians argued for many years. But luckily, the principle of infinity is stable now.

Ramen is doing some boring works on a kind of number string
S
n
Sn. To produce
S
n
Sn, write down all numbers from 1 to n in ascending order, then concatenate them directly. For example,
S
7

1234567
S7=1234567 and
S
15

123456789101112131415
S15=123456789101112131415.

Especially, the infinity number string is:
S

12345678910111213

S∞=12345678910111213⋯.

Ramen likes infinity, but he is bad at counting. He wants to know what the digit is at the position p in the infinity number string
S

S∞. Can you help him to solve this complex problem?
輸入描述:
The input contains multiple test cases.

The first line is an integer T(1 <= T <= 100000), which represents the number of test cases.

Each of the next T lines contains an integer p(1 <= p <= 1e18), represents the position has been asked.
輸出描述:
For each test case, output the digit at position p in
S

S∞ in one single line.
示例1
輸入
複製
5
1
10
19
66
1000000000000000000
輸出
複製
1
1
4
3
3
說明
The subsequence of
S

S∞ from 60 to 70 is:


53637383940

⋯53637383940⋯

It’s apparent that it’s 3 when p=66.

規律題了

import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.Scanner;
public class Main {
    public static Scanner in=new Scanner(new BufferedInputStream(System.in));
    public static long pow(long num,long l) {
        long sum = 1;
        for(int i=1;i<=l;i++) {
            sum*=num;
        }
        return sum;
    }
    
    public static void main(String[] args) {
        int t = in.nextInt();
        for(int i=1;i<=t;i++) {
            long n = in.nextLong();
            long sum = 0;
            long pre_sum = 0;
            long count = 1;
            while(true) {
                pre_sum = sum;
                sum+=(long)count*pow(10, count-1)*9;
                count++;
                if(sum >= n) {
                    break;
                }
            }
            count--;
            n -=pre_sum;
            long level =0;
            if(n%count == 0){
                level = n/count;
            }else{
                level = (n/count)+1;
            }
            long num = (long) Math.pow(10, count-1)+level-1;
            long rest = n%count;
            rest = (rest+count-1)%count;
            String num1 = String.valueOf(num);
            System.out.println(num1.charAt((int) rest));
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章