7-1 电话聊天狂人 (25 分)
#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
map<string,int>sp;
void solve(){
int n;
cin>>n;
string a,b;
for(int i=0;i<n;i++){
cin>>a>>b;
sp[a]++;
sp[b]++;
}
string s="";
int maxn=-1;
for(auto i:sp){
if(i.y>maxn){
maxn=i.y;
s=i.x;
}
}
int tot=0;
for(auto i:sp){
if(i.y==maxn&&i.x!=s) tot++;
}
if(!tot) cout<<s<<" "<<maxn<<endl;
else cout<<s<<" "<<maxn<<" "<<tot+1<<endl;
}
int main(){
solve();
return 0;
}
7-2 两个有序序列的中位数 (25 分)
#include<bits/stdc++.h>
const int N = 2e5 + 10;
#define x first
#define y second
using namespace std;
int a[N];
void solve(){
int n;
cin>>n;
for(int i=1;i<=2*n;i++) cin>>a[i];
sort(a+1,a+1+n*2);
cout<<a[n]<<endl;
}
int main(){
solve();
return 0;
}
7-3 词频统计 (30 分)
#include<bits/stdc++.h>
#define eb emplace_back
#define PSI pair<string,int>
const int N = 2e5 + 10;
#define x first
#define y second
using namespace std;
map<string,int>mp;
vector<PSI>vp;
bool cmp(PSI a,PSI b){
if(a.y!=b.y) return a.y>b.y;
else return a.x<b.x;
}
void solve(){
string s;
char c;
while(~scanf("%c",&c)&&c!='#'){
if(c>='a'&&c<='z'||c>='A'&&c<='Z'||c>='0'&&c<='9'||c=='_'){
if(c>='A'&&c<='Z') c+=32;
if(s.size()<15) s+=c;
}else{
if(s.size()>0){
mp[s]++;
s.clear();
}
}
}
for(auto i:mp){
vp.eb(PSI{i.x,i.y});
}
sort(vp.begin(),vp.end(),cmp);
cout<<vp.size()<<endl;
int len=vp.size()/10;
for(int i=0;i<len;i++){
cout<<vp[i].y<<":"<<vp[i].x<<endl;
}
}
int main(){
solve();
return 0;
}
7-4 集合相似度 (25 分)
#include<bits/stdc++.h>
#define eb emplace_back
#define PSI pair<string,int>
const int N = 2e5 + 10;
#define x first
#define y second
using namespace std;
set<int>sp[N];
void solve(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
int k,x;
cin>>k;
while(k--){
cin>>x;
sp[i].insert(x);
}
}
int m;
cin>>m;
while(m--){
int a,b;
int tot=0;
cin>>a>>b;
for(auto i:sp[a]){
if(sp[b].count(i)) tot++;
}
int sum=sp[a].size()+sp[b].size()-tot;
cout<<fixed<<setprecision(2)<<tot*100.0/sum<<"%"<<endl;
}
}
int main(){
solve();
return 0;
}
7-5 悄悄关注 (25 分)
#include <iostream>
#include<set>
#include<map>
#include<string>
#include<bits/stdc++.h>
#define ll long long
using namespace std;
set<string> s;
map<string,int>p,dp;
int main()
{
ios::sync_with_stdio(false);
int n;
cin>>n;
string name;
while(n--)
{
cin>>name;
p[name]=1;
}
int m;
cin>>m;
string a[11111];
int sum=0;
int k;
for(int i=0; i<m; i++)
{
cin>>a[i]>>k;
dp[a[i]]=k;
sum+=k;
}
sum/=m;
for(int i=0; i<m; i++)
{
if(!p[a[i]]&&dp[a[i]]>sum)
{
s.insert(a[i]);
}
}
if(s.empty())
{
cout<<"Bing Mei You"<<endl;
return 0;
}
set<string>::iterator it;
for(it=s.begin(); it!=s.end(); it++)
{
cout<<*it<<endl;
}
return 0;
}
7-6 单身狗 (25 分)
#include<bits/stdc++.h>
#define ll long long
const int N = 1e6 + 10;
using namespace std;
set<int>st;
set<int>sp;
int mp[N];
int dp[N];
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
int x,y;
cin>>x>>y;
mp[x]=y;
mp[y]=x;
}
int q;
cin>>q;
for(int i=1;i<=q;i++) cin>>dp[i],sp.insert(dp[i]);
for(int i=1;i<=q;i++){
if(!sp.count(mp[dp[i]])) st.insert(dp[i]);
}
cout<<(int)st.size()<<endl;
set<int>::iterator it;
for(it=st.begin();it!=st.end();it++){
if(it==st.begin()) printf("%05d",(*it));
else printf(" %05d",(*it));
}
return 0;
}
7-7 词典 (15 分)
#include<bits/stdc++.h>
#define eb emplace_back
#define PSI pair<string,int>
const int N = 2e5 + 10;
#define x first
#define y second
using namespace std;
map<string,string>mp;
void solve(){
int n,m;
cin>>n>>m;
string a,b;
for(int i=1;i<=n;i++){
cin>>a>>b;
mp[b]=a;
}
while(m--){
cin>>b;
if(mp[b]!="\0") cout<<mp[b]<<endl;
else cout<<"eh"<<endl;
}
}
int main(){
solve();
return 0;
}
7-8 中序遍历树并判断是否为二叉搜索树 (20 分)
#include<bits/stdc++.h>
#define eb emplace_back
#define PSI pair<string,int>
const int N = 2e5 + 10;
#define x first
#define y second
using namespace std;
vector<int>res,t;
struct tree{
int data;
tree *l,*r;
tree(int x){
data=x;
l=r=nullptr;
}
};
void midorder(tree *root){
if(!root) return ;
midorder(root->l);
cout<<root->data<<endl;
res.eb(root->data);
midorder(root->r);
}
void solve(){
unordered_map<int,tree*>mp;
int n;
cin>>n;
int x,d,e;
tree *root=nullptr;
for(int i=1;i<=n;i++){
cin>>x;
if(i>1){
cin>>d>>e;
auto r=mp[x];
t.eb(e);
if(!d){
r->l=new tree(e);
mp[e]=r->l;
}else{
r->r=new tree(e);
mp[e]=r->r;
}
}else{
root=new tree(x);
mp[x]=root;
t.eb(x);
}
}
midorder(root);
sort(t.begin(),t.end());
if(res==t) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
int main(){
solve();
return 0;
}