7-6 吉老师的回归 (15 point(s))
可能之前写的逻辑有bug,卡了一会
#include <bits/stdc++.h>
using namespace std;
const int N = 5e2 + 10;
int a[24];
string str[N];
int main(){
int n,m;
cin >> n >> m;
getchar();
int flag = 0;
int cnt = 0;
for(int i = 1; i <= n; i++){
getline(cin,str[i]);
if(str[i].find("easy") != -1 || str[i].find("qiandao") != -1 ){
cnt++;
}
else if(m != 0){
m--;
cnt++;
}
else if(m == 0){
cout << str[i] << endl;
flag = 1;
}
}
if(flag == 0) puts("Wo AK le");
// if(cnt >= n){
// puts("Wo AK le");
// }
// else{
// cout << str[cnt] << endl;
// }
return 0;
}
7-9 包装机 (25 point(s))
不那么粗糙版
#include <bits/stdc++.h>
using namespace std;
const int N = 1e2 + 10;
int n,m,s;
stack<char> st;
queue<char> ans;
queue<char> q[N];
int main(){
cin >> n >> m >> s;
for(int i = 1; i <= n; i++){
for(int j = 0; j < m; j++){
char x;
cin >> x;
q[i].push(x);
}
}
int num;
while(cin >> num && num != -1){
if(num == 0){
if(st.size() != 0){
char t = st.top();
st.pop();
ans.push(t);
}
}
else{
if(q[num].size()){
if(st.size() < s){
char t = q[num].front();
q[num].pop();
st.push(t);
}
else{
char t = st.top();
st.pop();
ans.push(t);
char tt = q[num].front();
q[num].pop();
st.push(tt);
}
}
}
}
while(ans.size()){
char t = ans.front();
ans.pop();
cout << t;
}
return 0;
}
粗糙版
栈和队列的模拟题
#include <bits/stdc++.h>
using namespace std;
const int N = 5e2 + 10;
int a[24];
string str[N];
map<int,int> mp;
stack<char> st;
queue<char> q[N];
queue<char> ans;
int main(){
int n,m,s;
cin >> n >> m >> s;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
char x;
cin >> x;
q[i].push(x);
}
}
int zl;
while(cin >> zl && zl != -1){
if(st.size() == 0 && zl == 0){
;
}
if(q[zl].size() == 0){
;
}
if(zl != 0 && q[zl].size() && st.size() < s){
char t = q[zl].front();
q[zl].pop();
st.push(t);
}
else if(zl != 0 && q[zl].size() && st.size() >= s){
char t = st.top();
st.pop();
ans.push(t);
char tt = q[zl].front();
q[zl].pop();
st.push(tt);
}
if(zl == 0){
if(st.size() == 0) ;
else{
char t = st.top();
st.pop();
ans.push(t);
}
}
}
while(ans.size()){
char t = ans.front();
ans.pop();
cout << t;
}
return 0;
}
7-10 病毒溯源 (25 point(s))
在输出字典序最小的路径这块,卡了很久。(说到底对dfs不熟练)
通过预处理,对子树的孩子们排序
又因为使用的是链式前向星,需要从大到小插入。
#include <bits/stdc++.h>
using namespace std;
const int N = 5e4 + 10;
int a[N];
int flag[N];//根节点
int ans;
int h[N],e[N],ne[N],idx;
bool vis;
vector<int> cc;
int path[N];
void add(int a, int b){
e[idx] = b, ne[idx] = h[a],h[a] = idx++;
}
void dfs(int u,int num){
for(int i = h[u]; i != -1; i = ne[i]){
int j = e[i];
dfs(j,num+1);
}
ans = max(ans,num);
return ;
}
void dfs2(int u,int num){
if(h[u] == -1 && num < ans) return ;
if(num == ans && !vis){
vis = 1;
return ;
}
for(int i = h[u]; i != -1; i=ne[i]){
int j = e[i];
if(!vis){
path[num] = j;
// cout << "num = "<< num << ". j = " << j << endl;
}
dfs2(j,num+1);
}
return ;
}
int q[N];
int main(){
int n;
cin >> n;
int k;
memset(h,-1,sizeof(h));
for(int i = 0; i < n; i++){
cin >> k;
for(int j = 0; j < k; j++){
cin >> a[j];
flag[a[j]] = true;
}
sort(a,a + k);
for(int j = k - 1; j >= 0; j--){
add(i,a[j]);
}
}
int root = -1;
for(int i = 0; i < n; i ++){
if(!flag[i]){
root = i;
}
}
dfs(root,1);
cout <<ans << endl;
// cout <<"root = " << root << endl;
dfs2(root,1);
path[0] = root;
for(int i = 0; i < ans; i++){
cout << path[i];
if(i != ans - 1) cout <<" ";
}
return 0;
}
7-11 清点代码库 (25 point(s))
学到了map的新用法。
以及发现自定义排序中,结构体中的向量数组可以直接用运算符排序。
在排序这一块,也卡了一会
#include <bits/stdc++.h>
using namespace std;
const int N = 5e4 + 10;
map<vector<int>,int> mp;
vector<int> b;
int n,m;
struct node {
vector<int> cc;
int num;
}aa[N];
bool cmp(node aaa, node bbb) {
if(aaa.num != bbb.num) {
return aaa.num > bbb.num;
}
return aaa.cc < bbb.cc;//这样直接排序,AC25分
//这样排序则只能得18分。
// for(int i = 0; i < m; i++) {
// return aaa.cc[i] < bbb.cc[i];
// }
}
int main() {
cin >> n >> m;
for(int i = 0; i < n; i++) {
vector<int> a;
for(int j = 0; j < m; j++) {
int x;
cin >> x;
a.push_back(x);
}
mp[a]++;
a.clear();
}
cout << mp.size() << endl;
map<vector<int>,int>::iterator it;
int k = 0;
for(it = mp.begin(); it != mp.end(); it++) {
aa[k].cc = it->first;
aa[k++].num = it->second;
}
sort(aa,aa + k, cmp);
for(int i = 0; i < k; i++) {
cout << aa[i].num <<" ";
for(int j = 0; j < m; j++) {
cout << aa[i].cc[j];
if(j != m - 1) {
cout <<" ";
}
}
cout << endl;
}
return 0;
}
7-14 还原文件 (35 point(s)) 暴力版(未AC)
暴力写的
只得了31分
,一个点超时。
去年,怎么就没下手呢。。。害,还是太菜了,爆哭。
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N];
int h[N],e[N],ne[N],idx;
int n,m;
bool flag[N];
void add(int a, int b){
e[idx] = b, ne[idx] = h[a],h[a] = idx++;
}
vector<int> ans[N];
int bb[N];
int kk;
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
cin >> m;
for(int i = 1; i <= m; i++){
int k;
cin >> k;
for(int j = 0; j < k ;j++){
int x;
cin >> x;
ans[i].push_back(x);
}
}
int i = 1;
while(i < n){
for(int j = 1; j <= m; j++){
if(flag[j]) continue;
int mar = 0;
for(int k = 0; k < (int)ans[j].size(); k++){
if(a[i + k] != ans[j][k]){
mar = 1;
break;
}
}
if(!mar && !flag[j]){
// cout <<" j = " << j ;
i += (int)ans[j].size() - 1;
// cout <<" i =" << i << endl;
bb[kk++] = j,flag[j] = true;
break;
}
}
}
for(int i = 0; i < kk; i++){
cout << bb[i];
if(i != kk - 1){
cout << " ";
}
}
return 0;
}