pat STL的应用

https://pintia.cn/problem-sets/994805342720868352/exam/problems/994805447855292416

 1 //需要注意的几个点
 2 //字符串哈希的应用
 3 //v.size的判断
 4 //对于选课人员的序号升序排序
 5 #include<bits/stdc++.h>
 6 using namespace std;
 7 #define int long long 
 8 #define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
 9 const int N=4e4+10;
10 const int M=26*26*26*10+1;
11 int n,k;
12 vector<int>v[M];
13 int getid(string s)
14 {
15     int id=0;
16     for(int i=0;i<3;i++)
17     {
18         id=id*26+(s[i]-'A');
19     }
20     id=id*10+(s[3]-'0');
21     return id;
22 }
23 signed main()
24 {
25     IOS;
26     cin>>n>>k;
27     while(k--)
28     {
29         string s;
30         int id,num;
31         cin>>id>>num;
32         for(int i=0;i<num;i++)
33         {
34             cin>>s;
35             int d=getid(s);
36             v[d].push_back(id);
37         }
38     }
39     string q;
40     for(int i=1;i<=n;i++)
41     {
42         cin>>q;
43         int d=getid(q);
44         sort(v[d].begin(),v[d].end());
45         cout<<q<<" ";
46         if(v[d].size())
47         {
48             cout<<v[d].size()<<" ";
49         }
50         else
51         {
52             cout<<v[d].size();
53         }
54         for(int j=0;j<v[d].size();j++)
55         {
56             cout<<v[d][j];
57             if(j<v[d].size()-1)
58                 cout<<" ";
59         }
60         cout<<endl;
61     }
62     return 0;
63 }

https://pintia.cn/problem-sets/994805342720868352/exam/problems/994805433955368960

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define int long long
 4 //#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
 5 const int N=4e4+10;
 6 vector<int>v[2510];
 7 char name[N][5];
 8 int n,k;
 9 int num;
10 bool cmp(int a,int b )
11 {
12     return strcmp(name[a],name[b])<0;
13 }
14 signed main()
15 {
16     //IOS;
17     ios::sync_with_stdio(false);
18     cin>>n>>k;
19     for(int i=0;i<n;i++)
20     {
21         int num;
22         cin>>name[i]>>num;
23         int tmp;
24         for(int j=0;j<num;j++)
25         {
26             cin>>tmp;
27             v[tmp].push_back(i);
28         }
29     }
30     for(int i=1;i<=k;i++)
31     {
32         cout<<i<<" "<<v[i].size()<<endl;
33         sort(v[i].begin(),v[i].end(),cmp);
34         for(int j=0;j<v[i].size();j++)
35         {
36             cout<<name[v[i][j]]<<endl;
37         }
38     }
39     return 0;
40 }

https://pintia.cn/problem-sets/994805342720868352/exam/problems/994805409175420928

 1 //题目大意:给定两个整数集合,它们的相似度定义为:Nc/Nt*100%。
 2 //其中Nc是两个集合都有的不相等整数的个数,Nt是两个集合一共有的不相等整数的个数。
 3 //你的任务就是计算任意一对给定集合的相似度
 4 //nc是两个集合的公共元素个数,nt是两个集合的所有包含的元素个数(其中元素个数表示各个元素之间互不相同)
 5 //很显然用set的查找函数,以一个集合为基准遍历在另一个集合中查找
 6 //最后计算的时候注意乘100即可
 7 #include<bits/stdc++.h>
 8 using namespace std;
 9 #define int long long 
10 #define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
11 const int N=110;
12 int n,m,k;
13 set<int>s[N];
14 double ans;
15 void solved(int x,int y)
16 {
17     double cnt1=0;
18     double cnt2=s[y].size();
19     for(auto &it:s[x])
20     {
21         if(s[y].find(it)!=s[y].end())
22         {
23             cnt1++;
24         }
25         else
26             cnt2++;
27     }
28     ans=cnt1*100.0/cnt2;
29 }
30 signed main()
31 {
32     //IOS;
33     cin>>n;
34     int tmp;
35     for(int i=1;i<=n;i++)
36     {
37         cin>>k;
38         for(int j=1;j<=k;j++)
39         {
40             cin>>tmp;
41             s[i].insert(tmp);
42         }
43     }
44     cin>>m;
45     int x,y;
46     while(m--)
47     {
48         ans=0;
49         cin>>x>>y;
50         solved(x,y);
51         printf("%.1f%\n",ans);
52     }
53     return 0;
54 }

https://pintia.cn/problem-sets/994805342720868352/exam/problems/994805422639136768

 1 //给定一个n*m的矩阵,要求统计个数最大的且站n*m/2以上的数
 2 //直接用map就行
 3 #include<bits/stdc++.h>
 4 using namespace std;
 5 #define int long long 
 6 #define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
 7 int m,n;
 8 unordered_map<int,int>q;
 9 set<int>s;
10 int maxx=-1;
11 int tmp;
12 signed main()
13 {
14     IOS;
15     cin>>m>>n;
16     for(int i=0;i<n;i++)
17     {
18         for(int j=0;j<m;j++)
19         {
20             cin>>tmp;
21             q[tmp]++;
22             s.insert(tmp);
23         }
24     }
25     for(auto &it:s)
26     {
27         if(q[it]>maxx&&q[it]>(n*m/2))
28         {
29             maxx=it;
30         }
31     }
32     cout<<maxx;
33     return 0;
34 }

https://pintia.cn/problem-sets/994805342720868352/exam/problems/994805398257647616

 1 //题目大意:给定一个字符串,规定合法单词是由小写和数字组成的单词,统计单词最多出现的次数
 2 //需要注意的点
 3 //输入一定要用getline
 4 //字符串t是用来储存合法单词的
 5 //isalnum是判断是否是单词或则是数字
 6 #include<bits/stdc++.h>
 7 using namespace std;
 8 #define int long long
 9 #define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
10 map<string,int>q;
11 int ans2;
12 string ans1;
13 string s;
14 string t;
15 signed main()
16 {
17     IOS;
18     getline(cin,s);
19     for(int i=0;i<s.length();i++)
20     {
21         if(isalnum(s[i]))
22         {
23             s[i]=tolower(s[i]);
24             t+=s[i];
25         }
26         if(!isalnum(s[i])||i==s.length()-1)
27         {
28             if(t.length()!=0)//注意排除空串的情况
29             {
30                 q[t]++;
31             }
32             t="";
33         }
34     }
35     for(auto &it:q)
36     {
37         if(it.second>ans2)
38         {
39             ans2=it.second;
40             ans1=it.first;
41         }
42     }
43     cout<<ans1<<" "<<ans2;
44     return 0;
45 }

 

posted @ 2022-12-02 15:34  江上舟摇  阅读(13)  评论(0编辑  收藏  举报