讲道理,这套题目很水,不适合练手……,但还是日常写一下报告吧,题目链接:点这里.
第一题
解析:
找一个字符串中第一次只出现一次的字母.这不瞎搞吗,直接用 hash 或 map 搞一下就可以了.
代码:
#include <bits/stdc++.h>
using namespace std;
#define MAX_BUF 1000005
char str[MAX_BUF];
int main()
{
int n;
for (scanf("%d", &n); n--;) {
scanf("%s", str);
map<char, int> mp;
for (int i = 0; str[i]; i++)
mp[str[i]]++;
for (int i = 0; str[i]; i++) {
if (mp[str[i]] == 1) {
cout << str[i] << endl;
break;
}
}
}
return 0;
}
第二题
解析:
找镇长,要求是:所有人都认识他,他不认识所有人,还问我们可以找到几个这样的候选镇长.很无语,这明显最多只能找到一个好吧,直接开数组存下他认识多少人,被多少人认识就好了.
代码:
#include <bits/stdc++.h>
using namespace std;
#define MAX_BUF 100005
int main()
{
int T;
for (scanf("%d", &T); T--;) {
int n, m;
scanf("%d%d", &n, &m);
int *in = new int[n + 1], *out = new int[n + 1];
for (int i = 0; i < n + 1; i++)
in[i] = out[i] = 1;
while (m--) {
int x, y;
scanf("%d%d", &x, &y);
if (x == y)
continue;
in[y]++;
out[x]++;
}
vector<int> ans;
for (int i = 1; i <= n; i++)
if (in[i] == n && out[i] == 1)
ans.push_back(i);
if (0 == ans.size())
cout << 0 << endl <<endl;
else {
cout << ans.size() << endl;
for (int i = 0; i < (int)ans.size(); i++)
cout << ans[i] << (i == (int)ans.size() - 1 ? "\n" : " ");
}
delete[] in;
delete[] out;
}
return 0;
}