【HDOJ】1857 Word Puzzle
trie树。以puzzle做trie树内存不够,从puzzle中直接找串应该会TLE。其实可以将查询组成trie树,离线做。
扫描puzzle时注意仅三个方向即可。
/* 1857 */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxr = ;
const int maxc = ;
const int maxn = 2e5+;
const int maxm = 1e4+;
char s[maxr][maxc], ss[maxc];
int X[maxn], Y[maxn];
int nxt[maxn][];
int P[maxm];
int l = ;
int wn = ;
int n, m; void init() {
memset(X, -, sizeof(X));
memset(Y, -, sizeof(Y));
} inline int newNode() {
return l++;
} void Insert(char *s) {
int i = , id;
int p = , q; while (s[i]) {
id = s[i] - 'A';
q = nxt[p][id];
if (!q)
q = nxt[p][id] = newNode();
p = q;
++i;
}
P[wn++] = p;
} void Find(char *s, int x, int y) {
int i = , id;
int p = ; while (s[i]) {
id = s[i] - 'A';
p = nxt[p][id];
if (!p)
return ;
if (X[p]< || x<X[p] || (x==X[p] && y<Y[p])) {
X[p] = x;
Y[p] = y;
}
++i;
}
} void solve() {
// horizontal
rep(i, , n) {
strcpy(ss, s[i]);
rep(j, , m)
Find(ss+j, i, j);
} // vertical
rep(j, , m) {
rep(i, , n)
ss[i] = s[i][j];
ss[n] = '\0';
rep(i, , n)
Find(ss+i, i, j);
} // diagonal
int x, y, l; rep(i, , n) {
x = i;
y = ;
l = ;
while (x<n && y<m) {
ss[l++] = s[x][y];
++x;
++y;
}
ss[l] = '\0';
rep(k, , l)
Find(ss+k, i+k, k);
} rep(j, , m) {
x = ;
y = j;
l = ;
while (x<n && y<m) {
ss[l++] = s[x][y];
++x;
++y;
}
ss[l] = '\0';
rep(k, , l)
Find(ss+k, k, j+k);
} } int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif init();
scanf("%d %d", &n, &m);
rep(i, , n)
scanf("%s", s[i]);
getchar();
getchar();
while (scanf("%s", ss)!=EOF && ss[]!='-') {
Insert(ss);
} solve(); int p;
rep(i, , wn) {
p = P[i];
printf("%d %d\n", X[p], Y[p]);
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
【HDOJ】1857 Word Puzzle的更多相关文章
- 【HDOJ】1462 Word Crosses
字符串水题,这么做可能比较巧妙. /* 1462 */ #include <iostream> #include <string> #include <map> # ...
- 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
[LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- 【HDOJ】1097 A hard puzzle
题目和1061非常相似,几乎可以复用. #include <stdio.h> ][]; int main() { int a, b; int i, j; ; i<; ++i) { b ...
- 【HDOJ】1098 Ignatius's puzzle
数学归纳法,得证只需求得使18+ka被64整除的a.且a不超过65. #include <stdio.h> int main() { int i, j, k; while (scanf(& ...
- 【HDOJ】3732 Ahui Writes Word
初看01背包,果断TLE.是因为n和C都比较大.但是vi和ci却很小,转化为多重背包. #include <cstdio> #include <cstring> ][]; ]; ...
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【HDOJ】1648 Keywords
PE的注意,如果没有满足条件的不输出空格.简单模拟,暴力解. /* */ #include <iostream> #include <sstream> #include < ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
- 【HDOJ】【3516】Tree Construction
DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...
随机推荐
- 通过拆分字段优化SQL
数据库环境:SQL SERVER 2008R2 今天看到一条用函数处理连接的SQL,是群里某位网友的,SQL语句如下: SELECT SO_Order.fdate , SO_Order.fsn FRO ...
- oc 通过webView调用js方法
- (void)viewDidLoad { [super viewDidLoad]; //加载本地web页面 web = [[UIWebView alloc]init]; web.background ...
- CSS高度塌陷
问题描述:当父元素只包含浮动的元素的时候,且父元素没有设置高度,如果父元素设置了边框border,那么看起来子元素不在父元素之内. 比如这样: html: <div id="paren ...
- intellij idea社区版 & maven & git & tomcat/jetty 的struts2项目的搭建
1.新建一个project,并在project下新建一个maven module.
- HDU 3943 K-th Nya Number(数位DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3943 题目大意:求出区间 (P,Q] 中找到第K个满足条件的数,条件是该数包含X个4和Y个7 Samp ...
- SQL 不同的数据类型
SQL 不同的数据类型 1.SQL TEXT 2.SQL VARCHAR(SIZE) VARCHAR(X) Case: user name, email, country, subject, pass ...
- <<深入Java虚拟机>>-虚拟机类加载机制-学习笔记
类加载的时机 遇到new.getstatic.putstatic或invokestatic这4个字节码指令时,如果类没有进行过初始化,则需要先触发其初始化.生成这4条指令最常见的Java场景是:使用n ...
- Linux之CentOS下vsftp安装及配置相关操作
1.安装ftps——vsftpd: #yum install vsftpd 2.指定上传下载目录配置: 如:用户名:xxx,需指定目录:/xxx/xxx #useradd -d /xxx/xxx -s ...
- memcached全面剖析--5
memcached的应用和兼容程序 mixi案例研究 mixi在提供服务的初期阶段就使用了memcached. 随着网站访问量的急剧增加,单纯为数据库添加slave已无法满足需要,因此引入了memca ...
- log4net 总结
说实话,我并不是太想写这篇文章,因为我承诺过要完成博客园的部分功能,所以一直都在积极的利用下班时间来完善这个系统, 但是我又不想让看我源代码的朋友不知道我写的代码是什么意思,所以我还是单独写一个文章, ...