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的更多相关文章

  1. 【HDOJ】1462 Word Crosses

    字符串水题,这么做可能比较巧妙. /* 1462 */ #include <iostream> #include <string> #include <map> # ...

  2. 【LeetCode】Longest Word in Dictionary through Deleting 解题报告

    [LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

  3. 【HDOJ】1097 A hard puzzle

    题目和1061非常相似,几乎可以复用. #include <stdio.h> ][]; int main() { int a, b; int i, j; ; i<; ++i) { b ...

  4. 【HDOJ】1098 Ignatius's puzzle

    数学归纳法,得证只需求得使18+ka被64整除的a.且a不超过65. #include <stdio.h> int main() { int i, j, k; while (scanf(& ...

  5. 【HDOJ】3732 Ahui Writes Word

    初看01背包,果断TLE.是因为n和C都比较大.但是vi和ci却很小,转化为多重背包. #include <cstdio> #include <cstring> ][]; ]; ...

  6. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  7. 【HDOJ】1648 Keywords

    PE的注意,如果没有满足条件的不输出空格.简单模拟,暴力解. /* */ #include <iostream> #include <sstream> #include < ...

  8. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  9. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

随机推荐

  1. HDU 4764 Stone(博弈)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4764 题目大意:Tang和Jiang玩石子游戏,给定n个石子,每次取[1,k]个石子,最先取完的人失败 ...

  2. vc实现ping

    //ping.h #ifndef _CPING_H_ #define _CPING_H_ #include <Winsock2.h> #include <Windows.h> ...

  3. [翻译][MVC 5 + EF 6] 11:实现继承

    原文:Implementing Inheritance with the Entity Framework 6 in an ASP.NET MVC 5 Application 1.选择继承映射到数据库 ...

  4. JSP九大内置对象和四个作用域

    JSP九大内置对象和四个作用域 在学习JSP的时候,首先就要先了解JSP的内置对象,什么是内置对象呢?内置对象也叫隐含对象,就是不需要预先声明就可以在脚本代码和表达式中随意使用.而这样的内置对象在JS ...

  5. mysql---索引及explain的作用

    索引:是一种数据结构,以增加存储开销和减慢DML(增.删.改)操作来提高查询速度. 常见的索引结构:btree索引(myisam,innodb,memory,heap),hash索引(memory,h ...

  6. 链表C++模板实现

    #include <iostream.h> #include <stdlib.h> //结点模板类 template <typename t1, typename t2& ...

  7. 支持HTML5新标签

    IE8/IE7/IE6支持通过document.createElement方法产生的标签,               可以利用这一特性让这些浏览器支持HTML5新标签,               ...

  8. Winfrom 中 ComboBox 绑定数据后设置选定项问题

    在为 ComboBox 当定数据的时候,如果遇到界面显示需要用文本,而获取选定项的值时需要用数字,我们就很习惯使用 DataSource  来进行绑定. 例如以下代码: List<TextVal ...

  9. php用正则表达式获取网站的标题内容

    已知网站的网址,用php获取网站的内容. 编写正则表达式. 用preg_match_all函数获取标题内容. $url='http://www.m-ivi.com'; $content=file_ge ...

  10. discuz 重新定义jquery的$

    最近做个小插件 发现加了这个代码不执行: $.ajax({ url:'plugin.php?id=register:regeist_jiangsu', type:'post', data:{ 'mob ...