https://codeforces.com/contest/1546/problem/B
INPUT
3
3 5
aaaaa
bbbbb
ccccc
aaaaa
bbbbb
3 4
aaaa
bbbb
cccc
aabb
bbaa
5 6
abcdef
uuuuuu
kekeke
ekekek
xyzklm
xbcklf
eueueu
ayzdem
ukukuk
OUTPUT
ccccc
cccc
kekeke
给T,给出T组数据,每组数据中给出n,m。 m是作为下面给出的字符串长度,然后是n行字符串,继而又是n - 1行字符串,在第一波的字符串和第二波的字符串差别在后者之中有一串没有,但在第二波的字符串其中存在许多对字符串进行同位交换字符串,你得输出后者缺少的那一串。
拿第二个样例举例
3 4
aaaa
bbbb
cccc
aabb
bbaa
在第一波中第一串的第三第四个位置和第二串的交换,得到aabb、bbaa,这两串在第二波中出现,所以在第一波中cccc为多余的(也就是第二波中没有的),所以输出cccc。
思路,纯暴力,后文还提供一种异或的方法(本人看不懂)
做个二位字符串数组,(因为题目中说n*m<1e5所以不用担心爆栈)
然后记录每个第一波字符串字母出现的个数,在第二波中减去出现的字母个数,最后剩下的就是多余的。
AC代码
#include<bits/stdc++.h>
using namespace std;
int cnt[100050][30];
int t;
int n,m;
int main()
{
cin>>t;
while(t--)
{
memset(cnt,0,sizeof(cnt));
cin>>n>>m;
char s1[n + 5][m + 5];
char s[m + 5];
for(int i = 0;i<n;i++)
{
cin>>s1[i];
for(int j = 0;j<m;j++)
cnt[j][s1[i][j] - 'a']++;
}
for(int i = 0;i < n - 1;i++)
{
cin>>s;
for(int j = 0;j<m;j++)
{
cnt[j][s[j] - 'a']--;
}
}
for(int i = 0;i<m;i++)
{
for(int j = 0;j<30;j++)
{
if(cnt[i][j])
{
s[i] = 'a' + j;
break;
}
}
}
s[m] = '\0'; //*
cout<<s<<endl;
}
return 0;
}
s[m] = ‘\0’; 这一个必须要,不然在第三轮会wa,虽然我不知道为啥,有明白的麻烦评论区留言哈
以下是异或方式做题的代码
#include <iostream>
#include <string>
#include <cstring>
// #define endl '\n'
#define i64 long long
const int MAXN = 5e5 + 10;
std::string s[MAXN];
char ans[MAXN];
void go() {
int n, m;
std::cin >> n >> m;
for (int i = 0; i < 2 * n - 1; ++i) std::cin >> s[i];
memset(ans, 0, sizeof(ans));
for (int j = 0; j < m; ++j) {
for (int i = 0; i < 2 * n - 1; ++i) {
ans[j] ^= s[i][j];
}
}
std::cout << ans << std::endl;
}
void run() {
int T;
std::cin >> T;
while (T-- > 0) {
go();
}
}
static const auto ___ = []() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); return 0; }();
int main() {
run();
return 0;
}
如有问题敬请指正,本人虚心受教