解题思路:两列两列的遍历,从上到下扫描各行,把每一行c1,c2两列的内容作为一个二元数组储存到一个map当中
(将输入的每一个不同的字符串分配一个编号(和集合栈计算机做法类似));如果map的键值中已经存在这个二元组
,则二元组映射的就是所求的r1,而当前行就是r2。
还需要注意一点:结构体用于map,set容器时必须重载<运算符,否则会出错,这是因为STL中的排序默认都是< 排序。可以通过<号找到相等元素,通过左右互换元素,不大于也不小于那肯定就是等于了。
- #include<iostream>
- #include<vector>
- #include<map>
- #include<string>
- using namespace std;
- const int row=10010;
- const int col=15;
- vector<int>table[row]; //用于储存表格中各字符串对应的数字编号
- vector<string>vec; //根据编号取字符串vec[int];
- map<string,int>mp; //将字符串映射对应编号
- int getID(string str){
- if(mp.count(str))return mp[str];
- else {
- vec.push_back(str);
- mp[str]=vec.size()-1;
- return mp[str];
- }
- }
- void read(int n){
- string str;
- getchar();//注意这里(对于每组数据,当输入数字后要用gerchar()吃掉换行,否则会错(就是这个错误找了很久))
- for(int i=0;i<n;i++){
- while(true){
- char ch=getchar();
- if(ch=='\n' || ch=='\r'){
- if(!str.empty()) table[i].push_back(getID(str));
- str.clear();
- break;}
- if(ch==','){
- table[i].push_back(getID(str));
- str.clear();
- }
- else str+=ch;
- }
- }
- }
- struct note{
- int x,y;
- bool operator<(const note & n)const {
- return this->x<n.x || this->x==n.x && this->y<n.y;
- }
- };
- map<note,int>mp2;
- void solve(int n,int m){
- for(int c=0;c<m;c++){
- for(int j=c+1;j<m;j++){
- mp2.clear(); //每两列结束都要清空map容器
- for(int r=0;r<n;r++){
- int c1=table[r][c];
- int c2=table[r][j];
- note nt;
- nt.x=c1;
- nt.y=c2;
- if(!mp2.count(nt))mp2[nt]=r;
- else {
- printf("NO\n");
- printf("%d %d\n",mp2[nt]+1,r+1);
- printf("%d %d\n",c+1,j+1);
- return ;
- }
- }
- }
- }
- printf("YES\n");
- }
- int main(){
- int n,m;
- while(cin>>n>>m){
- read(n);
- solve(n,m);
- for(int i=0;i<n;i++)table[i].clear();
- vec.clear();
- mp.clear();
- }
- return 0;
- }