题意:给出n行m列的字符串表,找出其中的两行,其相同位置的纵坐标(列)的字符串一样,输出NO以及该两行和该两列,没有这样的情况就输出YES。
思路:给每个字符串一个值,考虑到m较小,可以从列里入手判断。
# include <iostream>
# include <cstdio>
# include <cstring>
# include <map>
using namespace std;
map<string, int>M;
map<pair<int, int>, int>N;
int n, m, a[10003][11], cnt=0;
int fun(string s)
{
if(M.count(s))
return M[s];
M[s] = ++cnt;
return cnt;
}
int main()
{
freopen("database.in", "r", stdin);
freopen("database.out", "w", stdout);
scanf("%d%d",&n,&m);
char c = getchar();
for(int i=0; i<n; ++i)
{
int j=0;
string t;
while(c = getchar())
{
if(c == '\n')
{
a[i][j] = fun(t);
break;
}
else if(c != ',')
t += c;
else
{
a[i][j++] = fun(t);
t.clear();
}
}
}
for(int i=0; i<m; ++i)
for(int j=i+1; j<m; ++j)
{
for(int k=0; k<n; ++k)
{
pair<int,int>p = make_pair(a[k][i], a[k][j]);
if(N.count(p))
{
printf("NO\n%d %d\n%d %d\n",N[p]+1, k+1, i+1, j+1);
return 0;
}
N[p] = k;
}
N.clear();
}
puts("YES");
return 0;
}