HDU 4119Isabella's Message2011成都现场赛I题(字符串模拟)
Isabella's Message
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1377 Accepted Submission(s): 406
The encrypted message is an N * N matrix, and each grid contains a character.
Steve uses a special mask to work as a key. The mask is N * N(where N is an even number) matrix with N*N/4 holes of size 1 * 1 on it.
The decrypt process consist of the following steps:
1. Put the mask on the encrypted message matrix
2. Write down the characters you can see through the holes, from top to down, then from left to right.
3. Rotate the mask by 90 degrees clockwise.
4. Go to step 2, unless you have wrote down all the N*N characters in the message matrix.
5. Erase all the redundant white spaces in the message.
For example, you got a message shown in figure 1, and you have a mask looks like figure 2. The decryption process is shown in figure 3, and finally you may get a message "good morning".
You can assume that the mask is always carefully chosen that each character in the encrypted message will appear exactly once during decryption.
However, in the first step of decryption, there are several ways to put the mask on the message matrix, because the mask can be rotated (but not flipped). So you may get different results such as "od morning go" (as showed in figure 4), and you may also get other messages like "orning good m", "ng good morni".

Steve didn't know which direction of the mask should be chosen at the beginning, but after he tried all possibilities, he found that the message "good morning" is the only one he wanted because he couldn't recognize some words in the other messages. So he will always consider the message he can understand the correct one. Whether he can understand a message depends whether he knows all the words in the message. If there are more than one ways to decrypt the message into an understandable one, he will choose the lexicographically smallest one. 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.
Isabella sends letters to Steve almost every day. As decrypting Isabella's message takes a lot of time, and Steve can wait no longer to know the content of the message, he asked you for help. Now you are given the message he received, the mask, and the list of words he already knew, can you write a program to help him decrypt it?
Each test case contains several lines.
The first line contains an even integer N(2 <= N <= 50), indicating the size of the matrix.
The following N lines each contains exactly N characters, reresenting the message matrix. The message only contains lowercase letters and periods('.'), where periods represent the white spaces.
You can assume the matrix contains at least one letter.
The followingN lines each containsN characters, representing the mask matrix. The asterisk('*') represents a hole, and period('.') otherwise. The next line contains an integer M(1 <= M <= 100), the number of words he knew.
Then the following M lines each contains a string represents a word. The words only contain lowercase letters, and its length will not exceed 20.
If Steve cannot understand the message, just print the Y as "FAIL TO DECRYPT".
4
o.do
.ng.
grmn
o.i.
.*..
*.*.
....
*...
2
good
morning
4
..lf
eoyv
oeou
vrer
..*.
.*..
....
*.*.
5
i
you
the
love
forever
4
.sle
s.c.
e.fs
..uu
*...
.*..
...*
..*.
1
successful
Case #2: love you forever
Case #3: FAIL TO DECRYPT
题目大意:给一个n*n的矩阵,n为偶数,矩阵由小写字母和'.'组成,'.'表示空格,再给一个n*n矩阵,由'.'和'*'组成,'*'表示洞,'.'表示障碍。
现在将2张卡片重合,将能看到的字符从上往下从左往右依次取出组成一个新单词。卡片可以顺时针旋转90度,再取出能看到的
单词,一共有4个单词,4个单词再依次组成一个句子,因为卡片是连续转动的,所以4个单词首尾相连,任取一个做句子头,所
以句子一共也是4个。再给m个单词,求出字典序最小的句子并要求句子中每个单词都在给定的m个单词中。
坐标转换公式(i,j)——> (j,n-i+1) 坐标变换公式,从1,1开始。
先求出4个字符串连接好形成的字符串,然后改变连接顺序,组成新的4个字符串连接好形成的字符串。然后查找是否全部由单词本中的单词组成。(这段话来自于Hadis_yuki)
解题思路:开始是中间的连续的空格没有处理好,然后还有map没有初始化,结果WA出翔了。终于A了,当时比赛如果我做的话,可能会一WA到底。。需要修炼啊!
题目地址:Isabella's Message
AC代码:
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<cstdio>
#include<algorithm>
using namespace std;
char a[55][55]; //开始的地图
char tmp[55][55];
char mes[10005]; //存放连接好以后的字符串,还没有去空格
string res[5];
int visi[55][55]; //表示洞
int vis[55][55]; //visi的temp
int n,w;
map <string,int> word; //认识的单词 int cmp(string a,string b)
{
if(a<b) return 1;
return 0;
} void rotat() //顺时针旋转90度
{
int i,j;
memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(visi[i][j]==1)
vis[j][n+1-i]=1;
memset(visi,0,sizeof(visi));
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
visi[i][j]=vis[i][j];
} void solve()
{ int t=0; //如果存在一个满足条件则变为1
int i,j,k,l;
for(i=0;i<=3;i++)
res[i]=""; //每次都要清空
for(l=0;l<4;l++)
{
int flag=0;
k=4;
int p=0;
while(k--)
{
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(visi[i][j]==1)
mes[p++]=a[i][j];
rotat();
}
mes[p]='\0'; //mes是存放连接好以后的字符串,还没有去空格
//cout<<mes<<endl;
i=0;
while(i<p) //去前面的空格
{
if(mes[i]==' ')
i++;
else
break;
} j=p-1; //去后面的空格
while(j>=0)
{
if(mes[j]==' ')
j--;
else
break;
}
p=j;
//cout<<i<<" "<<p<<endl;
//cout<<mes<<endl;
char tmp1[55];
int tt=0;
for(j=i;j<=p;j++)
{
if(mes[j]==' '&&mes[j+1]==' ') //处理中间的空格
continue;
else if(mes[j]==' '||j==p)
{
if(j==p)
tmp1[tt++]=mes[j];
tmp1[tt]='\0';
//cout<<tt<<endl;
tt=0;
//cout<<tmp1<<endl;
if(word[tmp1])
{
if(res[t]=="")
res[t]=res[t]+tmp1;
else
res[t]=res[t]+" "+tmp1;
}
else
{
res[t]="";
flag=1;
break;
}
}
else
{
tmp1[tt++]=mes[j];
}
} if(!flag) t++;
rotat();
} if(t==0)
puts("FAIL TO DECRYPT");
else
{
sort(res,res+t,cmp);
cout<<res[0]<<endl;
}
} int main()
{
int tes,i,j;
scanf("%d",&tes);
for(int te=1;te<=tes;te++)
{
memset(visi,0,sizeof(visi));
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%s",a[i]+1); //a代表地图
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(a[i][j]=='.')
a[i][j]=' ';
for(i=1;i<=n;i++)
{
scanf("%s",tmp[i]+1);
for(j=1;j<=n;j++)
if(tmp[i][j]=='*')
visi[i][j]=1; //visi代表*
} scanf("%d",&w); //认识的单词数
string temp;
word.clear();
for(i=0;i<w;i++)
{
cin>>temp;
word[temp]=2;
}
//temp="}";
//word[temp]=2;
printf("Case #%d: ",te);
solve();
} /*int i,w;
scanf("%d",&w); //认识的单词数
string temp;
for(i=0;i<w;i++)
{
cin>>temp;
word[temp]=2;
}
temp="}";
word[temp]=2; char tm[20];
while(cin>>tm)
{
if(word[tm])
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}*/ return 0;
} /*
3
4
o.do
.ng.
grmn
o.i.
.*..
*.*.
....
*...
4
goo
morning
od
go
4
..lf
eoyv
oeou
vrer
..*.
.*..
....
*.*.
5
i
you
the
love
forever
4
.sle
s.c.
e.fs
..uu
*...
.*..
...*
..*.
1
successful
*/
HDU 4119Isabella's Message2011成都现场赛I题(字符串模拟)的更多相关文章
- hdu 4465 Candy 2012 成都现场赛
/** 对于大数的很好的应用,,缩小放大,,保持精度 **/ #include <iostream> #include <cmath> #include <algorit ...
- hdu 4788 (2013成都现场赛 H题)
100MB=10^5KB=10^8B 100MB=100*2^10KB=100*2^20B Sample Input2100[MB]1[B] Sample OutputCase #1: 4.63%Ca ...
- HDU 5120 A Curious Matt(2014北京赛区现场赛A题 简单模拟)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5112 解题报告:扫一遍 #include<cstdio> #include<cstr ...
- hdu 4813(2013长春现场赛A题)
把一个字符串分成N个字符串 每个字符串长度为m Sample Input12 5 // n mklmbbileay Sample Outputklmbbileay # include <iost ...
- hdu 5122 (2014北京现场赛 K题)
把一个序列按从小到大排序 要执行多少次操作 只需要从右往左统计,并且不断更新最小值,若当前数为最小值,则将最小值更新为当前数,否则sum+1 Sample Input255 4 3 2 155 1 2 ...
- ZOJ 3542 2011大连现场赛D题(简单模拟)
Hexadecimal View Time Limit: 2 Seconds Memory Limit: 65536 KB Hexadecimal is very important an ...
- 2011 ACM/ICPC 成都赛区(为2013/10/20成都现场赛Fighting)
hdu 4111 Alice and Bob 博弈:http://www.cnblogs.com/XDJjy/p/3350014.html hdu 4112 Break the Chocolate ...
- 2013杭州现场赛B题-Rabbit Kingdom
杭州现场赛的题.BFS+DFS #include <iostream> #include<cstdio> #include<cstring> #define inf ...
- Gym101981D - 2018ACM-ICPC南京现场赛D题 Country Meow
2018ACM-ICPC南京现场赛D题-Country Meow Problem D. Country Meow Input file: standard input Output file: sta ...
随机推荐
- 引用类型和原始类型的对比(java)
Java 提供两种不同的类型:引用类型和原始类型(或内置类型).另外,Java 还为每个原始类型提供了封装类(Wrapper). 原始类型 封装类=================boolean Bo ...
- 2.Android Studio系列教程2——基本设置与运行
原文链接:http://stormzhang.com/devtools/2014/11/28/android-studio-tutorial2/ 一.项目结构 二.Android Studio ...
- MySQL MyISAM/InnoDB高并发优化经验
最近做的一个应用,功能要求非常简单,就是 key/value 形式的存储,简单的 INSERT/SELECT,没有任何复杂查询,唯一的问题是量非常大,如果目前投入使用,初期的单表 insert 频率约 ...
- iOS 网络与多线程--5.异步Post方式的网络请求(非阻塞)
通过Post请求方式,异步获取网络数据,异步请求不会阻塞主线程,而会建立一个新的线程来操作. 代码如下 ViewController.h文件 #import <UIKit/UIKit.h> ...
- php学习之路
1.php拼接字符串+查询 $floor_id = M('house_floor_input')->where($map1)->field('id')->select(); $flo ...
- 你好,C++(28)用空间换时间 5.2 内联函数 5.3 重载函数
5.2 内联函数 通过5.1节的学习我们知道,系统为了实现函数调用会做很多额外的幕后工作:保存现场.对参数进行赋值.恢复现场等等.如果函数在程序内被多次调用,且其本身比较短小,可以很快执行完毕,那么 ...
- 控件真的很好用,突然感觉自己以前研究Discuz!NT366源码的方式很2了
控件真的很好用,突然感觉自己以前研究Discuz!NT366源码的方式很2了,就是按钮上的或其他控件上的图片哪里去了?
- DropdownList控件绑定数据源显示system.data.datarowview的问题
.net开发的时候经常需要用到在后台取数据再绑定到控件的问题,通常只需要连接数据库,从数据库取出数据,放到Dataset里面,然后再设置控件的DataSource为这个Dataset,然后再datab ...
- 用c++11打造类似于python的range
python中的range函数表示一个连续的有序序列,range使用起来很方便,因为在定义时就隐含了初始化过程,因为只需要给begin()和end()或者仅仅一个end(),就能表示一个连续的序列.还 ...
- MVC4,4月22日,Ninject的另外注入方式。
学习了Ninject另外两种绑定注入的方式: 1.根据属性绑定 先在特殊的实现借口类中定义属性 使用 2.根据构造函数方式绑定 学习了条件绑定方式(conditional bindi ...