没有l3
L1-6剪切粘贴
我也不会字符串,不过看数据范围很小, 就直接暴力for判断了吧.
不过t不能记录成0 啊,,,考试的时候不小心 最后得十三分
#include <iostream>
#include <cstring>
using namespace std;
int n, m;
string s, s1, s2, temp;
int main()
{
cin >> s >> n;
while (n --)
{
int l, r;
cin >> l >> r >> s1 >>s2;
temp = s.substr(l - 1, r - l + 1);
s = s.substr(0, l - 1) + s.substr(r, s.size() - r);
int t = -1;
for(int i = 0; s[i]; i ++)
{
bool flag = true;
if(s[i] == s1[0])
{
for(int j = 0; s1[j]; j ++)
{
if(s[i + j] != s1[j])
{
flag = false;
break;
}
}
if(!flag) continue;
for(int j = 0; s2[j]; j ++)
{
if(s[i + s1.size() + j] != s2[j])
{
flag = false;
break;
}
}
if(flag)
{
t = i + s1.size() - 1;
break;
}
}
}
if(t != -1)s = s.substr(0, t + 1) + temp + s.substr(t + 1, s.size() - t);
else s = s + temp;
}
cout << s <<endl;
return 0;
}
L1-7分寝室
一分是不能单人寝室
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
int n, n0, n1, cnt;
struct node{
int a, b, c;
bool operator < (const node &n) const
{
return c < n.c;
}
}a[1000000];
int main()
{
cin >> n0 >> n1 >> n;
for(int i = 1; i < n; i ++)
{
int j = n - i;
if(n0 % i != 0 || n1 % j != 0) continue;
if(n0 / i == 1 || n1 / j == 1) continue;
a[cnt].a = i, a[cnt].b = j, a[cnt].c = abs(n0 / i - n1 / j);
cnt ++;
}
sort(a, a + cnt);
if(!cnt) cout << "No Solution" << endl;
else cout << a[0].a << ' ' << a[0].b << endl;
return 0;
}
L1-8 谁管谁叫爹
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int n, a, b, c1, c2;
int main()
{
cin >> n;
while (n --)
{
c1 = 0;
c2 = 0;
cin >> a >> b;
int ta = a;
int tb = b;
while(ta)
{
c1 += ta % 10;
ta /= 10;
}
while(tb)
{
c2 += tb % 10;
tb /= 10;
}
if(a % c2 == 0 && b % c1 == 0)
{
if(a > b) cout << 'A' << endl;
else cout << 'B' << endl;
}
else if (a % c2 == 0) cout << 'A' << endl;
else if (b % c1 == 0) cout << 'B' << endl;
else
{
if(a > b) cout << 'A' << endl;
else cout << 'B' << endl;
}
}
return 0;
}
L2-1堆宝塔
模拟栈
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010;
int n, m;
int a[N];
int cnt, s;
int stka[N], stkb[N], t1, t2;
int main()
{
cin >> n;
for (int i = 0; i < n; i ++)
{
int t;
cin >> t;
if(!t1 || t < stka[t1 - 1]) stka[t1 ++] = t;
else
{
if(!t2 || stkb[t2 - 1] < t) stkb[t2 ++] = t;
else
{
cnt ++;
s = max(s, t1);
t1 = 0;
while(t2 && stkb[t2 - 1] > t)
{
stka[t1 ++] = stkb[t2 - 1];
t2 -- ;
}
stka[t1 ++ ] = t;
}
}
}
if(t1) cnt ++;
if(t2) cnt ++;
s = max(s, t1);
s = max(s, t2);
cout << cnt << ' ' << s << endl;
return 0;
}
stack
#include <iostream>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
const int N = 1010;
int n, t;
int cnt, s;
stack<int> a;
stack<int> b;
int main()
{
cin >> n;
for (int i = 0; i < n; i ++)
{
cin >> t;
if(a.size() == 0 || t < a.top()) a.push(t);
else
{
if (b.size() == 0 || t > b.top()) b.push(t);
else {
if(a.size() > s) s = a.size();
while(a.size()) a.pop();
cnt ++;
while(b.size() && b.top() > t)
{
a.push((b.top()));
b.pop();
}
a.push((t));
}
}
}
if(a.size() > s) s = a.size();
if(b.size() > s) s = b.size();
if(a.size()) cnt ++;
if(b.size()) cnt ++;
cout << cnt << ' ' << s << endl;
return 0;
}
L2-2 天梯赛的赛场安排
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int,int> PII;
const int N = 5010;
priority_queue<int>q;
int n, c;
string s[N];
int ans[N], res, jiaoshi[N], cnt;
int main()
{
cin >> n >> c;
for(int i = 1; i <= n; i ++)
{
int a;
cin >> s[i] >> a;
ans[i] += a / c;
a %= c;
res += ans[i];
if(a)
{
q.push(a);
ans[i] ++;
}
}
while(q.size())
{
int a = q.top();
q.pop();
for(int i = 0; i < cnt; i ++ )
{
if(jiaoshi[i] >= a)
{
jiaoshi[i] -= a;
a = 0;
break;
}
}
if(a) jiaoshi[cnt ++] = c - a;
}
for(int i = 1; i <= n; i ++)
{
cout << s[i] << ' ' << ans[i] << endl;
}
cout << res + cnt << endl;
return 0;
}
L2-3 锦标赛
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1 << 20;
int n;
int a[N], id[N]; //id 记录当前可以填的最小值
bool flag = true;
int main()
{
cin >> n;
for (int k = n; k >= 0; k --)
{
for(int i = 1 << k; i < 1 << (k + 1); i += 2) // 枚举该层的左节点, i表示左节点 i + 1 表示右节点
{
int t;
cin >> t;
if(t >= id[i]) a[i] = t;
else if(t > id[i + 1]) a[i + 1] = t;
else flag = false;
id[i] = max(id[i], t);
id[i + 1] = max(id[i + 1], t);
id[i / 2] = max(id[i], id[i + 1]);
}
}
if(!flag) cout << "No Solution" << endl;
else
{
for(int i = 1; i < 1 << n; i ++)
{
if(!a[i * 2]) a[i * 2] = a[i];
else if(!a[i * 2 + 1]) a[i * 2 + 1] = a[i];
}
for (int i = 1 << n; i < 1 << (n + 1); i ++)
{
if (i == 1 << n) cout << a[i];
else cout << ' ' << a[i];
}
}
return 0;
}
L2-4 寻宝图
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 100010;
int n, m;
string s[N];
int cnt1, cnt2;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, 1, -1};
bool check(int x, int y)
{
if(x >= 0 && y >= 0 && x < n && y < m && s[x][y] != '0') return true;
return false;
}
void bfs(int x, int y)
{
cnt1 ++;
bool flag = false;
if(s[x][y] > '1') flag = true;
s[x][y] = '0';
queue<PII> q;
q.push({x, y});
while(q.size())
{
PII t = q.front();
q.pop();
for(int i = 0; i < 4; i ++)
{
int nx = t.first + dx[i];
int ny = t.second + dy[i];
if(check(nx, ny))
{
q.push({nx, ny});
if(s[nx][ny] > '1') flag = true;
s[nx][ny] = '0';
}
}
}
if(flag) cnt2 ++;
}
int main()
{
cin >> n >> m;
for(int i = 0; i < n; i ++)
{
cin >> s[i];
}
for(int i = 0; i < n; i ++)
{
for (int j = 0; j < m; j ++)
{
if(s[i][j] != '0')bfs(i, j);
}
}
cout << cnt1 << ' ' << cnt2 << endl;
return 0;
}