HJ71~HJ80 华为机试题库
HJ71 字符串通配符
题目:https://www.nowcoder.com/practice/43072d50a6eb44d2a6c816a283b02036?tpId=37&tqId=21294&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=
先读入两个字符串a,b,然后进行大小写转换,统一转化成小写。字符串下标从1开始存储。dp[i][j]表示对于a的前i位字符串和b的前j位字符串是否能匹配,如果可以则为1,否则为0。
处理边界情况:dp[0][0]=1;即两串字符串都为空字符串时是匹配的。然后for(int i=1;i<=la;i++)if(a[i]=='*')dp[i][0]=1;else break;由于*可以对应空字符串,所以***的情况是可以和空字符串匹配的,所以要预处理为1.
状态转移方程:
- `a[i] == b[j]` 或 `a[i] == '?'` 且 `b[j]` 是合法字符时,`dp[i][j] = dp[i-1][j-1]`。
- `a[i] == '*'` 且 `b[j]` 是合法字符时:
- `dp[i][j] = dp[i-1][j-1]`:`*`匹配一个字符
- `dp[i][j] = dp[i][j-1]`:`*`匹配多个字符
- `dp[i][j] = dp[i-1][j]`:`*`匹配空字符
1 #include<bits/stdc++.h> 2 using namespace std; 3 char a[110],b[110]; 4 int la,lb,dp[110][110]; 5 bool Check(char c){ 6 if(c>='a'&&c<='z')return 1; 7 if(c>='0'&&c<='9')return 1; 8 return 0; 9 } 10 int main(){ 11 scanf("%s",a+1); 12 scanf("%s",b+1); 13 la=strlen(a+1); lb=strlen(b+1); 14 for(int i=1;i<=la;i++) 15 if(a[i]>='A'&&a[i]<='Z')a[i]=a[i]-'A'+'a'; 16 for(int i=1;i<=lb;i++) 17 if(b[i]>='A'&&b[i]<='Z')b[i]=b[i]-'A'+'a'; 18 dp[0][0]=1; 19 for(int i=1;i<=la;i++) if(a[i]=='*')dp[i][0]=1;else break; 20 for(int i=1;i<=la;i++){ 21 for(int j=1;j<=lb;j++){ 22 if(a[i]==b[j])dp[i][j]=max(dp[i][j],dp[i-1][j-1]); 23 else if(a[i]=='?'&&Check(b[j]))dp[i][j]=max(dp[i][j],dp[i-1][j-1]); 24 else if(a[i]=='*'&&Check(b[j])){ 25 dp[i][j]=max(dp[i][j],dp[i-1][j-1]); 26 dp[i][j]=max(dp[i][j],dp[i][j-1]); 27 dp[i][j]=max(dp[i][j],dp[i-1][j]); 28 } 29 } 30 } 31 if(dp[la][lb])puts("true");else puts("false"); 32 }
HJ72 百钱买百鸡问题
https://www.nowcoder.com/practice/74c493f094304ea2bda37d0dc40dc85b?tpId=37&tqId=21295&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=
不是……???这个题目………………就直接让我打表啊?????
1 #include<bits/stdc++.h> 2 using namespace std; 3 int main(){ 4 puts("0 25 75"); 5 puts("4 18 78"); 6 puts("8 11 81"); 7 puts("12 4 84"); 8 }
HJ73 计算日期到天数转换
https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded?tpId=37&tqId=21296&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D2%26tpId%3D37%26type%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=
1 #include<bits/stdc++.h> 2 using namespace std; 3 int days[12]={31,28,31,30,31,30,31,31,30,31,30,31}; 4 int main(){ 5 int y,m,d,sum=0; 6 cin>>y>>m>>d; 7 for(int i=1;i<m;i++){ 8 if(i!=2)sum+=days[i-1]; 9 else { 10 if((y%100&&y%4==0)||(y%100==0&&y%400==0)) 11 sum+=29;else sum+=28; 12 } 13 } 14 sum+=d; 15 cout<<sum; 16 }
HJ74 参数解析
题目:https://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677?tpId=37&tqId=21297&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=
用stringstream流处理数据,可以方便地提取里面的string> <
1 #include<bits/stdc++.h> 2 using namespace std; 3 int main(){ 4 string s; 5 getline(cin,s); 6 stringstream v; 7 v<<s; 8 string w; 9 int cnt=0; 10 while(v>>w){ 11 if(w[0]!='\"')cnt++; 12 else{ 13 if(w[w.size()-1]=='\"')cnt++; 14 else{ 15 while(v>>w){ 16 if(w[w.size()-1]=='\"')break; 17 } 18 cnt++; 19 } 20 } 21 } 22 cout<<cnt<<endl; 23 v.clear();v<<s; 24 while(v>>w){ 25 if(w[0]!='\"')cout<<w<<endl; 26 else { 27 if(w[w.size()-1]=='\"'){ 28 w.erase(0,1); 29 w.erase(w.size()-1,1); 30 cout<<w<<endl; 31 } 32 else{ 33 w.erase(0,1); 34 cout<<w; 35 while(v>>w){ 36 if(w[w.size()-1]!='\"')cout<<" "<<w; 37 else { 38 w.erase(w.size()-1,1); 39 cout<<" "<<w<<endl; 40 break; 41 } 42 } 43 } 44 45 } 46 } 47 }
HJ75 公共子串计算
https://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b?tpId=37&tqId=21298&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=
1 #include<bits/stdc++.h> 2 using namespace std; 3 int main(){ 4 int ans=0; 5 string s1,s2; 6 cin>>s1>>s2; 7 int l1=s1.size(),l2=s2.size(); 8 vector<vector<int>>dp(l1+1,vector<int>(l2+1)); 9 for(int i=0;i<l1;i++){ 10 for(int j=0;j<l2;j++){ 11 if(s1[i]==s2[j]){ 12 if(i-1>=0&&j-1>=0) dp[i][j]=dp[i-1][j-1]+1; 13 else dp[i][j]=1; 14 } 15 else dp[i][j]=0; 16 ans=max(ans,dp[i][j]); 17 } 18 } 19 cout<<ans; 20 }
HJ76 尼科彻斯定理
https://www.nowcoder.com/practice/dbace3a5b3c4480e86ee3277f3fe1e85?tpId=37&tqId=21299&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=
1 #include<bits/stdc++.h> 2 using namespace std; 3 int main(){ 4 int m;cin>>m; 5 int m_2=m*m; 6 if(m_2%2){ 7 int fr=m_2,cnt=1,to_cnt=m/2; 8 for(;cnt<=to_cnt;cnt++) 9 fr-=2; 10 cnt=1; 11 for(;cnt<m;cnt++){ 12 cout<<fr<<"+"; 13 fr+=2; 14 } 15 cout<<fr; 16 } 17 else{ 18 int fr=m_2-1,cnt=1,to_cnt=m/2; 19 for(;cnt<to_cnt;cnt++) 20 fr-=2; 21 cnt=1; 22 for(;cnt<m;cnt++){ 23 cout<<fr<<"+"; 24 fr+=2; 25 } 26 cout<<fr; 27 } 28 }
HJ77 火车进站
https://www.nowcoder.com/practice/97ba57c35e9f4749826dc3befaeae109?tpId=37&tqId=21300&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=
用字典序dfs出出站序列,然后用Check函数判断是否合法。如果不合法及时终止。
非法的序列满足:晚出现的排在前,但在它之后仍然存在符合进队序列次序的对子。
id记录各个号出现的位置。
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,id[11]; 4 bool book[11]; 5 bool Check(int num,const vector<int>&a){ 6 if(num==0) return 1; 7 for(int i=0;i<num;i++){ 8 int wz=id[a[i]]; 9 for(int j=i+1;j<num;j++){ 10 for(int k=j+1;k<num;k++){ 11 if(id[a[k]]<wz&&id[a[j]]<id[a[k]])return 0; 12 } 13 } 14 } 15 return 1; 16 } 17 void dfs(int x,vector<int>&a){ 18 if(!Check(x,a))return; 19 if(x==n){ 20 for(auto i:a)cout<<i<<" "; 21 cout<<endl; 22 return; 23 } 24 for(int i=1;i<=n;i++){ 25 if(book[i]==0){ 26 book[i]=1; 27 a.push_back(i); 28 dfs(x+1,a); 29 a.pop_back(); 30 book[i]=0; 31 } 32 } 33 } 34 int main(){ 35 cin>>n; 36 int x; 37 for(int i=1;i<=n;i++){ 38 cin>>x; 39 id[x]=i; 40 } 41 vector<int>a; 42 dfs(0,a); 43 }
HJ80 整型数组合并
https://www.nowcoder.com/practice/c4f11ea2c886429faf91decfaf6a310b?tpId=37&tqId=21303&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D2%26tpId%3D37%26type%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=
set基本操作
1 #include<bits/stdc++.h> 2 using namespace std; 3 int main(){ 4 int n; 5 cin>>n; 6 set<int>st; 7 int x; 8 for(int i=1;i<=n;i++){ 9 cin>>x; 10 st.insert(x); 11 } 12 cin>>n; 13 for(int i=1;i<=n;i++){ 14 cin>>x; 15 st.insert(x); 16 } 17 for(auto i:st)cout<<i; 18 }
by:AlenaNuna