错误代码
hack input:
4
66
66
**
66
66
**
66
66
**
66
66
hack output:
1
wrong output:
0
wrong code:
#include <iostream>
#include <sstream>
#include <unordered_set>
using namespace std;
int n;
unordered_set <string> s;
int main () {
cin >> n;
for (int i = 1;i <= n;i++) {
string a,b;
cin >> a >> b;
a += b;
s.insert (a);
for (int i = 1;i <= 3;i++) {
stringstream ss;
ss << a[2] << a[0] << a[3] << a[1];
a = ss.str ();
s.insert (a);
}
cin >> a;
}
cout << s.size () / 4 << endl;
return 0;
}
思路
我们用$unordered~set$判断是否重复,每次判断矩阵的四种形态是否出现过,没有就加到$unordered~set$里。
代码
#include <iostream>
#include <sstream>
#include <unordered_set>
using namespace std;
int n;
unordered_set <string> s;
int main () {
cin >> n;
for (int i = 1;i <= n;i++) {
string a,b;
cin >> a >> b;
a += b;
if (s.count (a)) {
cin >> a;
continue;
}
bool flag = false;
for (int i = 1;i <= 4;i++) {
stringstream ss;
ss << a[2] << a[0] << a[3] << a[1];
a = ss.str ();
if (s.count (a)) {
flag = true;
break;
}
}
if (!flag) s.insert (a);
cin >> a;
}
cout << s.size () << endl;
return 0;
}
错误代码解析
下面的代码没有考虑到一下样例:
4
66
66
**
66
66
**
66
66
**
66
66
hh大佬我看到你代码了,应该吧错误代码放上面更明显一些,第一遍看都没往下划
好的,我改一下
hh,要多想点