Or just write a programming problem. This wrote about a half hour. . Just finished the afternoon.
Still a bit difficult. . QAQ I would not really string /
Questions link: 2020 recruit school systems development direction of pen questions
7, large numbers adder
The meaning of problems: Read a string of numbers into two, and then outputs two numbers in a string.
Solution: This problem I've ever done! ! ! Before joining Blue Bridge when the cup of java with learned 23333 (it should be said that since the cup to go to Hubei headlines today, the court press 8-bit precision c ++ board is grass back go for a java large numbers ...)
Application of large integer class of java , the details can see links, there are many common uses of java.
This question pit is that it is quote as a character input. . ! QAQ so to remove the quotation marks.
Code:
1 import java.util.Scanner; 2 import java.math.BigInteger; 3 4 public class Main{ 5 public static void main(String[] args) { 6 Scanner scanner = new Scanner(System.in); 7 String num1 = scanner.nextLine(); 8 String num2 = scanner.nextLine(); 9 num1 = num1.substring(1,num1.length()-1); 10 num2 = num2.substring(1,num2.length()-1); 11 //初始化 12 BigInteger n1 = new BigInteger(num1); 13 BigInteger n2 = new BigInteger(num2); 14 15 //System.out.println(n1); 16 //System.out.println(n2); 17 BigInteger sum = n1.add(n2); //加 18 19 System.out.println("\""+sum+"\""); 20 } 21 22 }
8, Kaibunko skewer
Meaning of the questions: Given a string, your task is to calculate how many palindrome substring (string palindrome is a positive reading and verlan same string) string there.
Palindrome sequence having a different start position or the end position, even by the same characters will be counted as a different string.
Solution: The problem is the longest palindrome substring deformation, can we make a statistic, the longest palindromic substring can see links.
Code:
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1010; 4 int dp[maxn][maxn] = {0}; 5 6 int main(){ 7 string s; 8 cin>>s; 9 int len = s.size(); 10 int cnt = 0; 11 cnt += len; 12 for(int i = 0; i < len ;i++){ 13 dp[i][i] = 1; 14 if(s[i] == s[i+1]){ 15 dp[i][i+1] = 1; 16 cnt++; 17 } 18 } 19 for(int pos = 3; pos<=len ; pos++){ 20 for(int i = 0 ;i <= len-pos; i++){ 21 int j = i+pos-1; 22 if(dp[i+1][j-1] && s[i] == s[j]){ 23 dp[i][j]=1; 24 cnt++; 25 } 26 } 27 } 28 29 cout<<cnt<<endl; 30 31 return 0; 32 }
9, the merger gold
The meaning of problems: there are N stack coins in a row, the i-heap is C [i] pieces of gold. Each time the merger will be merged into the adjacent piles pile of gold coins, which cost as the number of blocks and piles of gold coins. N-1 times after the merger, will eventually all be combined into a pile of gold coins. Please find the lowest cost gold combined into a pile of.
其中,1 <= N <= 30,1 <= C[i] <= 100
Solution: previous play when training have done a stone called the merger of the title. A classic interval dp applications.
Written explanations in detail later finished up.
Code:
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn = 105; 4 5 int n; 6 int num[maxn]={0}; 7 int dp[maxn][maxn]={0}; 8 int cost[maxn][maxn]={0}; 9 10 int main(){ 11 cin>>n; 12 for(int i = 1;i <= n;i++) cin>>num[i]; 13 14 memset(dp,0x3f,sizeof(dp)); 15 16 for(int i = 1; i <= n;i++){ 17 for(int j = i; j <= n ;j++){ 18 for(int k = i; k <= j; k++){ 19 cost[i][j] += num[k]; 20 } 21 } 22 } 23 24 for(int i = 1; i <= n ;i++) dp[i][i] = 0; 25 26 for(int len = 2; len <= n;len++){ 27 for(int l = 1; l <= n-len+1 ;l++){ 28 int r = l+len-1; 29 for(int k = l ; k < r; k++){ 30 dp[l][r] = min(dp[l][r],dp[l][k] + dp[k+1][r]+cost[l][r]); 31 } 32 } 33 } 34 35 cout<<dp[1][n]<<endl; 36 return 0; 37 }
10, the smallest unique prefix
The meaning of problems: given a set of string, find the smallest possible to uniquely identify the prefix string for each string.
Solution: when the mind is thought trie, but nothing and to do violence. . But the violence is not beautiful. .
So on the board three minutes on the AC. / Trie tree is actually pretty good. . Ready to string up more questions. . I really weak string.
Code:
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 const int maxn = 105; 5 const int maxnode = 100005; 6 7 string str[maxn]; 8 9 int ch[maxn][maxn]; 10 char val[maxn]; 11 12 struct Trie { 13 int sz; 14 Trie() { 15 sz = 1; 16 memset(ch[0], 0, sizeof(ch[0])); 17 } 18 int idx(char c) { return c - 'a'; } 19 20 void insert(string s) { 21 int u = 0; 22 int n = s.size(); 23 for(int i = 0; i < n; i++) { 24 int c = idx(s[i]); 25 if(!ch[u][c]) { 26 memset(ch[sz], 0, sizeof(ch[sz])); 27 val[sz] = 0; 28 ch[u][c] = sz++; 29 } 30 u = ch[u][c]; 31 val[u]++; 32 } 33 } 34 35 void query(string s) { 36 int u = 0; 37 int n = s.size(); 38 for(int i = 0; i < n; i++) { 39 putchar(s[i]); 40 int c = idx(s[i]); 41 if(val[ch[u][c]] == 1) return ; 42 u = ch[u][c]; 43 } 44 } 45 }; 46 47 int main() { 48 Trie trie; 49 int n; 50 cin>>n; 51 for(int i = 0;i < n ;i++){ 52 cin>>str[i]; 53 trie.insert(str[i]); 54 } 55 for(int i = 0; i < n; i++) { 56 trie.query(str[i]); 57 cout<<endl; 58 } 59 return 0; 60 }