目录
1. 亚运会奖牌榜
输入样例:
15
0 1
0 2
0 3
0 1
0 1
0 2
0 3
1 3
1 3
1 3
1 3
1 2
1 1
1 1
1 1
输出样例:
3 2 2
3 1 4
The first win!
具体代码实现:
vector<int>类型通过简单的'>'和‘<’刚好可以实现题目所说的比较
#include<bits/stdc++.h>
using namespace std;
vector<int> a,b;
int r1[4],r2[4];
int main(void){
int n;
cin>>n;
for(int i=0;i<n;i++){
int c,p;
cin>>c>>p;
if(c==0){ //第一个国家
r1[p]++;
}else{
r2[p]++;
}
}
for(int i=1;i<4;i++){
if(i!=1) cout<<" ";
cout<<r1[i];
}
cout<<endl;
for(int i=1;i<4;i++){
if(i!=1) cout<<" ";
cout<<r2[i];
}
cout<<endl;
for(int i=1;i<4;i++){
a.push_back(r1[i]);
b.push_back(r2[i]);
}
if(a>b)
cout<<"The first win!";
else
cout<<"The second win!";
return 0;
}
2. 出院
输入样例:
5 6
Diet A
LowSugarTea B
Milk C
Coke D
Water A
DietCoke
Pepsi
Milk
CokeWater
GoodMilk
dietCoke
输出样例:
AD
D
C
DA
D
D
具体代码实现:
根据题意:拆分字符串只能拆分成两部分
//思路:因为这个字符串只能被拆成两个,
//那么我们就直接使用指针将其拆成两个,判断是否存在即可
#include<bits/stdc++.h>
using namespace std;
map<string,string>mp; //存储对应关系
int n,m;
int main(void){
cin>>n>>m;
for(int i=0;i<n;i++){
string s1,s2;
cin>>s1>>s2;
mp[s1]=s2;
}
for(int i=0;i<m;i++){
if(i!=0)
cout<<endl;
string s;
cin>>s;
//1.是已知等级的饮料
if(mp.count(s)){
cout<<mp[s];
}else{
//2.由两个数组成
int cnt=0; //表示匹配的次数
string ans;
for(int i=1;i<s.size();i++){
string s1=s.substr(0,i);
string s2=s.substr(i);
if(mp.count(s1)&&mp.count(s2)){ //正确拆分
cnt++;
ans=mp[s1]+mp[s2];
}
}
if(cnt==0||cnt>1)
cout<<"D";
else cout<<ans;
}
}
return 0;
}
count用来统计是否存在在该键值对的键里面。
3. 骰子游戏
4. 相对论大师
输入样例:
5
Yu 0 Yuci 0
Rou 1 Yu 1
Yuci 0 Rou 1
Yuci 0 Gutou 0
Gutou 0 Rou 0
输出样例:
Yu 0 Yuci 0 Yuci 0 Rou 1 Rou 1 Yu 1 = Yu 0 Yu 1
具体代码实现:
构图+bfs深度遍历
//思路:
//构图:yu 0为一个整体
//bfs找最短路径
#include<bits/stdc++.h>
using namespace std;
const int N=1100;
int n; //推论有n条,点有n+1个
map<string,vector<string> >f; //存储有向图关系
map<string,int> vis;
map<string,string> pre;
vector<string> s; //存储具体路径
//求最短路径
int bfs(string st,string ed){
queue<string> q;
for(auto& x:vis){
x.second=-1;
}
vis[st]=0;
q.push(st);
while(!q.empty()){
string x=q.front();
q.pop();
if(x==ed){
return 1;
}
vector<string> v=f[x];
for(int i=0;i<v.size();i++){
if(vis[v[i]]==-1){
vis[v[i]]=vis[x]+1;
pre[v[i]]=x;
q.push(v[i]);
}
}
}
return -1;
}
int main(void){
cin>>n;
//1.构图
for(int i=0;i<n;i++){ //n行
string a,b,c,d;
cin>>a>>b>>c>>d;
string s1=a+" "+b;
string s2=c+" "+d;
f[s1].push_back(s2);
vis[s1]=-1,vis[s2]=-1;
}
//2.穷举
string nst,ned; //最终开始点和终点
int nd=2000; //最终最短长度
for(auto x:vis){
string st=x.first;
char t=(st[st.size()-1]=='0')?'1':'0';
string ed=st.substr(0,st.size()-1)+t;
if(vis.count(ed)){ //存在终点
if(bfs(st,ed)!=-1)
if(nd>vis[ed]){
nd=vis[ed];
nst=st;
ned=ed;
}
}
}
//3.求具体路径
bfs(nst,ned);
s.push_back(ned);
string t=ned;
while(t!=nst){
t=pre[t];
s.push_back(t);
}
reverse(s.begin(),s.end());
for(int i=0;i<s.size();i++){
if(i!=0) cout<<" ";
cout<<s[i];
if(i>0&&i<s.size()-1){
cout<<" "<<s[i];
}
}
cout<<" = "+nst+" "+ned<<endl;
return 0;
}
5. 相对成功与相对失败
输入样例:
3
5
1 0
1 0
0 0
0 0
0 1
1 2 3 4 5
5
1 0
1 0
0 0
0 0
0 1
5 4 3 2 1
5
1 0
0 1
0 0
0 1
1 1
4 2 1 3 5
输出样例:
0
3
2
具体代码实现:
//思路:我们可以计算一个得分,不玩手机和参加比赛各加一分
//根据所给排序可以得到得分序列(逆序),因为必须是一个不下降子序列
//我们可以把这个的最长不下降子序列求出来,然后用序列的长度-最长,就是不符合要求的
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n;
int a[N],f[N]; //按顺序存储每个人的得分,所给顺序存储
int dp[N]; //dp[i]表示以i为最长序列的最小尾数
int main(void){
int t;
cin>>t;
for(int i=1;i<=t;i++){
memset(a,0,sizeof(a));
cin>>n;
//1.获得得分
for(int i=1;i<=n;i++){
int temp=0;
int b1,b2;
cin>>b1>>b2;
if(b1==1)
temp++;
if(b2==0)
temp++;
a[i]=temp;
}
//2.按所给顺序倒叙存储
for(int i=n;i>0;i--){
int b;
cin>>b;
f[i]=a[b];
}
//3.求最长不下降子序列(使用二分nlogn->因为是逆序存储)
dp[1]=f[1];
int res=1;
//找第一个>f[i]的dp
for(int i=2;i<=n;i++){
int p=upper_bound(dp+1,dp+res+1,f[i])-dp;
dp[p]=f[i];
res=max(res,p);
}
if(i!=1) cout<<endl;
cout<<n-res;
}
return 0;
}
最后,附上题目链接:PTA | 程序设计类实验辅助教学平台 (pintia.cn)
都看到这了,点个赞再走吧!!!😊