一道当时没能AC的题,今天做例题9-2想起来了。。
预处理将字符串转为整数(与例题9-2和这章之前的集合栈计算机类似)
枚举列比枚举行快的多,每次枚举用扫描法+map判重(以后有机会像Morris一样用hash做一做)
最近没怎么做字符串的题,被字符串输入坑了几个WA,写在这里免得以后再犯:
scanf("%s", str) 这种输入方法是直到空格、回车和其他不可打印字符停止。如果中间有空格的情况下想一直读到行尾,要么从头到尾读一整行然后逐个字符处理(个人认为很呆),要么用正则:scanf("%[^\n]", str)
Run Time: 2.922s
#define UVa "LT5-9.1592.cpp" //Database
char fileIn[30] = UVa, fileOut[30] = UVa;
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
#include<string>
using namespace std;
int main() {
const int maxn = 10000 + 10, maxm = 10 + 10;
int n,m; //n: # of rows. m: # of cols.
while(scanf("%d%d", &n, &m) != EOF) {
getchar();
int db[maxn][maxm];
char entry[100], comma;
map<string, int> dict;
int cnt = 1;
for(int i = 0; i < n; i ++) {
for(int j = 0; j < m-1; j ++) {
scanf("%[^,]%c", entry, &comma);
string s(entry);
if(!dict.count(s)) dict[s] = cnt ++;
db[i][j] = dict[s];
}
scanf("%[^\n]", entry);
getchar();
string s(entry);
if(!dict.count(s)) dict[s] = cnt ++;
db[i][m-1] = dict[s];
}
int ok = 0;
for(int c1 = 0; c1 < m; c1 ++) {
for(int c2 = c1 + 1; c2 < m; c2 ++) {
map<pair<int,int>, int > vis;
for(int r2 = 0; r2 < n; r2 ++) {
pair<int,int> p(db[r2][c1],db[r2][c2]);
if(vis.count(p)) {
ok = 1;
printf("NO\n%d %d\n%d %d\n", vis[p]+1,r2+1,c1+1,c2+1);
break;
}
vis[p] = r2;
}
if(ok) break;
}
if(ok) break;
}
if(!ok) printf("YES\n");
}
return 0;
}