A-1 Braille Recognition
两层循环遍历,用数组计数
#include <bits/stdc++.h>
using namespace std;
int a[10];
int main() {int n,m;cin>>n>>m;string s[110];for(int i=0;i<n;i++)cin>>s[i];for(int i=0;i<n-2;i++) {for(int j=0;j<m-1;j++) {if(s[i][j]=='*'&&s[i][j+1]=='.'&&s[i+1][j]=='.'&&s[i+1][j+1]=='.'&&s[i+2][j]=='.'&&s[i+2][j+1]=='.')a[1]++;if(s[i][j]=='*'&&s[i][j+1]=='.'&&s[i+1][j]=='*'&&s[i+1][j+1]=='.'&&s[i+2][j]=='.'&&s[i+2][j+1]=='.')a[2]++;if(s[i][j]=='*'&&s[i][j+1]=='*'&&s[i+1][j]=='.'&&s[i+1][j+1]=='.'&&s[i+2][j]=='.'&&s[i+2][j+1]=='.')a[3]++;if(s[i][j]=='*'&&s[i][j+1]=='*'&&s[i+1][j]=='.'&&s[i+1][j+1]=='*'&&s[i+2][j]=='.'&&s[i+2][j+1]=='.')a[4]++;if(s[i][j]=='*'&&s[i][j+1]=='.'&&s[i+1][j]=='.'&&s[i+1][j+1]=='*'&&s[i+2][j]=='.'&&s[i+2][j+1]=='.')a[5]++;if(s[i][j]=='*'&&s[i][j+1]=='*'&&s[i+1][j]=='*'&&s[i+1][j+1]=='.'&&s[i+2][j]=='.'&&s[i+2][j+1]=='.')a[6]++;if(s[i][j]=='*'&&s[i][j+1]=='*'&&s[i+1][j]=='*'&&s[i+1][j+1]=='*'&&s[i+2][j]=='.'&&s[i+2][j+1]=='.')a[7]++;if(s[i][j]=='*'&&s[i][j+1]=='.'&&s[i+1][j]=='*'&&s[i+1][j+1]=='*'&&s[i+2][j]=='.'&&s[i+2][j+1]=='.')a[8]++;if(s[i][j]=='.'&&s[i][j+1]=='*'&&s[i+1][j]=='*'&&s[i+1][j+1]=='.'&&s[i+2][j]=='.'&&s[i+2][j+1]=='.')a[9]++;if(s[i][j]=='.'&&s[i][j+1]=='*'&&s[i+1][j]=='*'&&s[i+1][j+1]=='*'&&s[i+2][j]=='.'&&s[i+2][j+1]=='.')a[0]++;}}for(int i=1;i<=9;i++)cout<<a[i]<<" ";cout<<a[0];
}
A-2 AI Comments
对所有考生的每一个维度排序,取中位数
对于每组查询,判断准考证号存不存在,存在的话于中位数比较,把{{mi-vi, i}, i}加入map(根据key, value升序排序)
#include <bits/stdc++.h>
using namespace std;
map<string,int>s;vector<int>a[5],sa[5];
int main() {int n,m,av[5];cin>>n>>m;for(int i=0;i<n;i++) {string ss;cin>>ss;s[ss]=i;for(int j=0;j<5;j++) {int aa;cin>>aa;a[j].push_back(aa);}}for(int i=0;i<5;i++) {sa[i]=a[i];sort(sa[i].begin(),sa[i].end());av[i]=sa[i][n/2];}while(m--) {string ss;cin>>ss;if(s.find(ss)==s.end()) {cout<<"Not Found"<<endl;continue;}map<pair<int,int>,int>z,f;for(int i=0;i<5;i++) {if(a[i][s[ss]]>=av[i])z[{av[i]-a[i][s[ss]],i}]=i+1;else f[{av[i]-a[i][s[ss]],i}]=i+1;}for(auto it=z.begin();it!=z.end();it++) {if(it!=z.begin())cout<<" ";cout<<it->second;}if(!z.empty()&&!f.empty())cout<<" ";for(auto it=f.begin();it!=f.end();it++) {if(it!=f.begin())cout<<" ";cout<<"-"<<it->second;}cout<<endl;}
}
A-3 Degree of Skewness
深度优先搜索
后序遍历最后一个结点为根结点,在中序遍历序列中找到根节点的位置,然后判断有没有左右子树。
#include <bits/stdc++.h>
using namespace std;
int n,po[1010],in[1010],l,r;
void f(int pl,int pr,int il,int ir) {int p;for(int i=il;i<=ir;i++) {if(in[i]==po[pr])p=i;}if(p>il)f(pl,pl+p-il-1,il,p-1);if(p<ir)f(pl+p-il,pr-1,p+1,ir);if(p==il&&p!=ir)r++;if(p==ir&&p!=il)l++;
}
int main() {cin>>n;for(int i=1;i<=n;i++)cin>>po[i];for(int i=1;i<=n;i++)cin>>in[i];f(1,n,1,n);cout<<l-r<<" = "<<l<<" - "<<r;
}
A-4 Uniqueness of Topological Order
首先算出每个结点的入度,输出入度最小的结点输出,把入度为0的结点入队
①没有入度为0的点,不存在拓扑序列
之后广搜,搜索过的结点后继结点入度减一,入度为0则入队
②队列中结点不唯一,拓扑序列不唯一
③如果有结点没有入队,不存在拓扑序列
#include <bits/stdc++.h>
using namespace std;
vector<int>v[10010];
int in[10010],mi=999999,f,r=1/*是否存在拓扑序列*/,num/*加入拓扑序列的结点数*/;
vector<int>res;
int main() {int n,m,mi=999999;cin>>n>>m;while(m--) {int x,y;cin>>x>>y;v[x].push_back(y);in[y]++;}queue<int>q;for(int i=1;i<=n;i++) {if(in[i]==0)q.push(i); // 入度为0的结点入队mi=min(mi,in[i]); // 所有结点入度的最小值}for(int i=1;i<=n;i++) {if(in[i]==mi) { // 输出入度最小的结点if(f==1)cout<<" ";cout<<i;f=1;}}cout<<endl;if(mi!=0)r=0; // 1.没有入度为0的结点,不存在拓扑序列while(!q.empty()) {if(q.size()!=1) { // 2.拓扑序列不唯一r=0;break;}int t=q.front();q.pop();res.push_back(t); // 加入拓扑序列for(int i=0;i<v[t].size();i++) {in[v[t][i]]--; // 入度减一if(in[v[t][i]]==0) { // 为0则入队q.push(v[t][i]);}}num++; // 拓扑序列的结点}if(num!=n)r=0; // 3.如果有结点没在序列,不存在拓扑序列cout<<(r==1?"Yes":"No")<<endl;if(r==1) {cout<<res[0];for(int i=1;i<n;i++) {cout<<" "<<res[i];}}
}