【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[ ...
随机推荐
- iOS获取webview高度
int webHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scr ...
- leetcode之Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- C# 多任务之 Task
Task 是什么 ? Task 是一个类, 它表示一个操作不返回一个值,通常以异步方式执行. Task 对象是一个的中心思想 基于任务的异步模式 首次引入.NET Framework 4 中. 继承层 ...
- 第46条:for-each循环优先于传统的for循环
for-each循环通过完全隐藏迭代器或者索引变量,避免混乱和出错的可能,适用于集合和数组和任何实现Iterable接口的对象. 使用传统for循环,容易出错: enum Face { ONE, TW ...
- mysql常见的hint(转载的)
mysql常用的hint对于经常使用oracle的朋友可能知道,oracle的hint功能种类很多,对于优化sql语句提供了很多方法.同样,在mysql里,也有类似的hint功能.下面介绍一些常用的. ...
- jquery 在页面中按回车 响应 事件
为了用户方便我们往往会在用户回车之后做一些事,比如登陆的时候,填完表单过后,我们习惯性的会直接按回车,当然要处理这个,jquery是很简单的,我们来看看怎么做吧. $(document).ready( ...
- Oracle if else if for case
------------------游标+for+if else if DECLARE cursor s_cursor is SELECT * from emp;--定义游标 begin for r ...
- linux日志审计项目案例实战(生产环境日志审计项目解决方案)
所谓日志审计,就是记录所有系统及相关用户行为的信息,并且可以自动分析.处理.展示(包括文本或者录像) 推荐方法:sudo配合syslog服务,进行日志审计(信息较少,效果不错) 1.安装sudo命令. ...
- Python设计模式——装饰模式(Decorator)
假如我们需要开发一个程序来展示一个人穿衣服的过程. #encoding=utf-8 __author__ = 'kevinlu1010@qq.com' class Person(): def __in ...
- nodejs 第一次使用
在win7下安装与使用 1 nodejs官网下载,安装 https://nodejs.org/ 2 下载最新的 npm,在E:\nodejs\中解压 http://nodejs.org/dist/ ...