模拟题,用STL可以快速A掉。
我傻×的读错题目了,耗了好长时间……注意题目这句话:The way to compare two messages is to compare the words of two messages one by one, and the first pair of different words in the two messages will determine the lexicographic order of them.
字典序大小的比较方式是一个一个单词的比!
#include<cstdio>
#include<string>
#include<cstring>
#include<set>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=112;
set<string>myset;
vector<string>ans[maxn];
int d[maxn][maxn],tt[maxn][maxn];
char a[maxn][maxn];
char rtmp[maxn];
int n;
void cpy(int x[][maxn],int b[][maxn])
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
x[i][j]=b[i][j];
}
void rotate(int x[][maxn])
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
tt[j][n-i+1]=x[i][j];
cpy(x,tt);
}
int main()
{
int sec;
scanf("%d",&sec);
for(int z=1;z<=sec;z++)
{
int tot=0;
myset.clear();
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",rtmp);
for(int j=1;j<=n;j++)
a[i][j]=rtmp[j-1];
}
for(int i=1;i<=n;i++)
{
scanf("%s",rtmp);
for(int j=1;j<=n;j++)
if(rtmp[j-1]=='*')d[i][j]=1;//dong
else d[i][j]=0;
}
int m;
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
string s;
cin>>s;
myset.insert(s);
}
int tmp[maxn][maxn];
cpy(tmp,d);
for(int w=1;w<=4;w++)
{
cpy(d,tmp);
string t;t.clear();
for(int q=1;q<=4;q++)
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(d[i][j]==1) t.push_back(a[i][j]);
rotate(d);
}
int l=t.length();
bool flag=true;
string s;s.clear();
tot++;ans[tot].clear();
for(int i=0;i<l;i++)
{
if(t[i]!='.')s.push_back(t[i]);
if((t[i]=='.'||i==l-1)&&s.length()>0)
{
if(myset.count(s)<=0){flag=false;break;}
else ans[tot].push_back(s);
s.clear();
}
}
if(!flag)
tot--;
rotate(tmp);
}
printf("Case #%d: ",z);
if(tot==0)cout<<"FAIL TO DECRYPT\n";
else
{
int k=1;
for(int i=2;i<=tot;i++)
{
for(int j=0;j<ans[i].size();j++)
if(ans[i][j]<ans[k][j]){k=i;break;}
else if(ans[i][j]>ans[k][j])break;
}
//
for(int i=0;i<ans[k].size();i++)
if(i+1!=ans[k].size())cout<<ans[k][i]<<" ";
else cout<<ans[k][i]<<"\n";
}
}
return 0;
}