HDU4119 -Isabella's Message
2011 Asia ChengDu Regional Contest
题意:给定一个加密的字符矩阵,以及一个解密的矩阵,以及能够认识的单词,通过矩阵中的洞对应后可以知道原来的字符,顺时针旋转90度后四次可以得到加密的信息,输出解密后的能够理解的字典序最小的信息,能理解是指信息中的单词全部认识。
分析:按题意直接模拟即可,首先解密信息,有四种开始解密的方式,最后只有4个字符串,看哪个字符串中全部的单词都认识即可,刚开始的时候弄错了题意,以为得到的信息中的字母可以按得到的顺序任意组成单词,半天一直在想怎么处理这个问题,后面通过第二个样例才知道肯定想错了,信息中连续在一起的字母才算是单词…..所以问题变得很简单了,只要直接判断就好了,处理掉多余的空格,后面查找就行了。
其实感觉处理这个问题的方式有很多种,但是不同的处理方式效率与正确性甚至复杂度却大大不同,所以考虑不同的方式时还是注意编码复杂性与效率的问题,况且在方便地使用STL容器的同时本身就需要注意到效率的问题,不过这题数据量小,就无所谓。
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
#include <string>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
struct h{
int x, y;
}pos[700];
bool cmp(h a, h b)
{
if (a.x != b.x) return a.x < b.x;
return a.y < b.y;
}
char mat[60][60], ho[60][60];
string temp[10];
set<string> word;
void deal(string& x)
{
while (x[0] == ' ') x.erase(0, 1);
while (x[x.size()-1] == ' ') x.erase(x.size()-1, 1);
int len = x.size();
for (int i = 0; i < len; ){
if (x[i] == ' ' && x[i+1] == ' ') x.erase(i, 1), len--;
else i++;
}
}
int main()
{
int t, n, m, ca = 0;
scanf("%d", &t);
while (t--){
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%s", mat[i]+1);
for (int i = 1; i <= n; i++) scanf("%s", ho[i]+1);
scanf("%d", &m);
word.clear();
for (int i = 0; i < m; i++){
char s[30];
scanf("%s", s); word.insert(s);
}
int k = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (ho[i][j] == '*') pos[k++] = (h){i, j};
fill(temp, temp+10, "");
for (int i = 0; i < k; i++){
if (mat[pos[i].x][pos[i].y] == '.') temp[0] += ' ';
else temp[0] += mat[pos[i].x][pos[i].y];
}
for (int i = 1; i < 4; i++){
for (int j = 0; j < k; j++){
swap(pos[j].x, pos[j].y);
pos[j].y = n+1-pos[j].y;
}
sort(pos, pos+k, cmp);
for (int j = 0; j < k; j++){
if (mat[pos[j].x][pos[j].y] == '.') temp[i] += ' ';
else temp[i] += mat[pos[j].x][pos[j].y];
}
}
string tm[10];
for (int i = 0; i < 4; i++){
tm[i] = "";
for (int x = i, cnt = 0; cnt < 4; cnt++, x = (x+1)%4) tm[i] += temp[x];
deal(tm[i]);
}
sort(tm, tm+4);
string ans = "";
for (int i = 0; i < 4; i++){
int it[100];
int l = 1;
it[0] = -1;
for (int j = 1; j < tm[i].size(); j++)
if (tm[i][j] == ' ') it[l++] = j;
it[l] = tm[i].size();
int tag = 1;
for (int j = 0; j < l && tag; j++)
if (!word.count(tm[i].substr(it[j]+1, it[j+1]-it[j]-1))) tag = 0;
if (tag) {ans = tm[i]; break;}
}
printf("Case #%d: ", ++ca);
ans.size() ? puts(ans.c_str()) : puts("FAIL TO DECRYPT");
}
return 0;
}