p1984_因为没有仔细看样例导致题目理解错误

解决在一个由多个农场组成的网格中,根据道路连接信息实时计算任意两个农场之间的曼哈顿距离的问题。涉及处理动态输入数据,并在数据不完整的情况下给出最优解答。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Navigation Nightmare
Time Limit: 2000MSMemory Limit: 30000K
Total Submissions: 1345Accepted: 551
Case Time Limit: 1000MS

Description

Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1..N. A series of M (1 <= M < 40,000) vertical and horizontal roads each of varying lengths (1 <= length <= 1000) connect the farms. A map of these farms might look something like the illustration below in which farms are labeled F1..F7 for clarity and lengths between connected farms are shown as (n):
           F1 --- (13) ---- F6 --- (9) ----- F3

| |

(3) |

| (7)

F4 --- (20) -------- F2 |

| |

(2) F5

|

F7

Being an ASCII diagram, it is not precisely to scale, of course.

Each farm can connect directly to at most four other farms via roads that lead exactly north, south, east, and/or west. Moreover, farms are only located at the endpoints of roads, and some farm can be found at every endpoint of every road. No two roads cross, and precisely one path
(sequence of roads) links every pair of farms.

FJ lost his paper copy of the farm map and he wants to reconstruct it from backup information on his computer. This data contains lines like the following, one for every road:

There is a road of length 10 running north from Farm #23 to Farm #17
There is a road of length 7 running east from Farm #1 to Farm #17
...

As FJ is retrieving this data, he is occasionally interrupted by questions such as the following that he receives from his navigationally-challenged neighbor, farmer Bob:

What is the Manhattan distance between farms #1 and #23?

FJ answers Bob, when he can (sometimes he doesn't yet have enough data yet). In the example above, the answer would be 17, since Bob wants to know the "Manhattan" distance between the pair of farms.
The Manhattan distance between two points (x1,y1) and (x2,y2) is just |x1-x2| + |y1-y2| (which is the distance a taxicab in a large city must travel over city streets in a perfect grid to connect two x,y points).

When Bob asks about a particular pair of farms, FJ might not yet have enough information to deduce the distance between them; in this case, FJ apologizes profusely and replies with "-1".

Input

* Line 1: Two space-separated integers: N and M



* Lines 2..M+1: Each line contains four space-separated entities, F1,

F2, L, and D that describe a road. F1 and F2 are numbers of

two farms connected by a road, L is its length, and D is a

character that is either 'N', 'E', 'S', or 'W' giving the

direction of the road from F1 to F2.



* Line M+2: A single integer, K (1 <= K <= 10,000), the number of FB's

queries



* Lines M+3..M+K+2: Each line corresponds to a query from Farmer Bob

and contains three space-separated integers: F1, F2, and I. F1

and F2 are numbers of the two farms in the query and I is the

index (1 <= I <= M) in the data after which Bob asks the

query. Data index 1 is on line 2 of the input data, and so on.

Output

* Lines 1..K: One integer per line, the response to each of Bob's

queries. Each line should contain either a distance

measurement or -1, if it is impossible to determine the

appropriate distance.

Sample Input

7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
3
1 6 1
1 4 3
2 6 6

Sample Output

13
-1
10

Hint

At time 1, FJ knows the distance between 1 and 6 is 13.
At time 3, the distance between 1 and 4 is still unknown.
At the end, location 6 is 3 units west and 7 north of 2, so the distance is 10.

Source


想了一会,感觉是lca 和 树状数组的组合,然后开始敲,后来发现答案不对,就开始找错误,偶然间发现不能用树状数组。
又感觉到并查集,继续敲,后来样例过不了。
看了好久程序,后来发现程序没有毛病,是理解错了。。。。
这样的事情发生的不是一次两次了。。。这样不好~
tt加油!
``` #include <bits/stdc++.h> using namespace std; int n; string s[1000005],t; struct trie { int nxt[1000005][26],times[1000005]; int sum[1000005],fail[1000005],in[1000005],sz; queue <int> q; int pos(string s,int len) { int p = 0; for (int i = 0;i < len;i++) { if (!nxt[p][s[i]-'a']) return 0; p = nxt[p][s[i]-'a']; } return p; } void insert(string s,int len) { int p = 0; for (int i = 0;i < len;i++) { if (!nxt[p][s[i]-'a']) nxt[p][s[i]-'a'] = ++sz; p = nxt[p][s[i]-'a']; } times[p]++; } void kmp() { memset(fail,0,sizeof(fail)); for (int i = 0;i < 26;i++) if (nxt[0][i]) q.push(nxt[0][i]); while (!q.empty()) { int i = q.front(); q.pop(); for (int j = 0;j < 26;j++) if (nxt[i][j]) { int p = fail[i]; while (p>0 && !nxt[p][j]) p = fail[p]; if (nxt[p][j]) p = nxt[p][j]; fail[nxt[i][j]] = p,in[p]++; q.push(nxt[i][j]); } } } void search(string s,int len) { memset(sum,0,sizeof(sum)); int p = 0; for (int i = 0;i < len;i++) { while (!nxt[p][s[i]-'a'] && p>0) p = fail[p]; if (!nxt[p][s[i]-'a']) continue; p = nxt[p][s[i]-'a'],sum[p]++; } for (int i = 0;i < sz;i++) if (in[i] == 0) q.push(i); while (!q.empty()) { int f = q.front(); q.pop(); while (f) { if (in[fail[f]] == 1) q.push(in[fail[f]]); sum[fail[f]] += sum[f],in[fail[f]]--,f = fail[f]; } } } void clear() { sz = 0; memset(nxt,0,sizeof(nxt)); memset(times,0,sizeof(times)); } } tree; int main() { cin >> n; for (int i = 1;i <= n;i++) cin >> s[i]; for (int i = 1;i <= n;i++) tree.insert(s[i],s[i].length()); cin >> t; tree.kmp(),tree.search(t,t.length()); for (int i = 1;i <= n;i++) cout << tree.sum[tree.pos(s[i],s[i].length())] << endl; return 0; }```题目描述 给你一个文本串 S 和 n 个模式串 T1∼n​,请你分别求出每个模式串 Ti​ 在 S 中出现的次数。 输入格式 第一行包含一个正整数 n 表示模式串的个数。 接下来 n 行,第 i 行包含一个由小写英文字母构成的非空字符串 Ti​。 最后一行包含一个由小写英文字母构成的非空字符串 S。 数据不保证任意两个模式串不相同。 输出格式 输出包含 n 行,其中第 i 行包含一个非负整数表示 Ti​ 在 S 中出现的次数。 输入: 5 a bb aa abaa abaaa abaaabaa 输出: 6 0 3 2 1 输出不对,请分析代码哪里有错误
03-10
c++解决 # 题目背景 ## 题目描述 星野加奈给你一个 $n$ 个点的无向图,图初始没有边。他还有整数 $u,v$ 和 $a_1,a_2,\cdots ,a_n$。现在有 $q$ 次操作,操作有四种: 1. ```1 x y``` :连接 $x,y$ 之间的边,保证边原先不存在。 2. ```2 x y``` :删除 $x,y$ 之间的边,保证边原先存在。 3. ```3 x y``` :将 $a_x$ 修改为 $y$ 。 4. ```4 x``` :设图分为 $C_1,C_2,\cdots ,C_k$ 共 $k$ 个连通块,求出 $\sum_{i=1}^k \prod_{j\in C_i}(a_j+x) \bmod u^v$。 ## 输入格式 第一行四个整数 $n,q,u,v$。 第二行 $n$ 个整数 $a_1,a_2,\cdots , a_n$。 接下来 $q$ 行,每行表示一次操作。 ## 输出格式 若干行,每行一个整数,表示每次 $4$ 操作的答案。 ## 输入输出 #1 ### 输入 #1 ``` 5 10 3 2 1 2 3 4 5 4 2 1 1 2 1 3 4 4 0 1 2 3 3 2 5 4 1 2 3 4 1 4 5 4 2 ``` ### 输出 #1 ``` 7 1 3 3 ``` ## 说明/提示 Idea:忘记来源了,请当时的出题人 qq 私信我 ### 二 见附件中的 `ex_c2.in` 和 `ex_c2.ans`,此满足子任务 $1$。 ### 三 见附件中的 `ex_c3.in` 和 `ex_c3.ans`,此满足子任务 $2$。 ### 四 见附件中的 `ex_c4.in` 和 `ex_c4.ans`,此满足子任务 $6$。 ### 限制与约定 本题采用捆绑测试。 对于 $100\%$ 的数据,满足 $1\leq n,q\leq 10^5,1\leq u\leq 10,1\leq v\leq 4,0\leq a_i <10^4$,$3$ 操作中 $y$、$4$ 操作中 $x$ 均为小于 $10^4$ 的非负整数。 | 子任务编号 | 分值 | $n\leq$ | $q\leq$ | 特殊性质 | | :--------- | ---- | ------- | ------- | ------------------------------------ | | $1$ | $20$ | $5000$ | $5000$ | $/$ | | $2$ | $10$ | $10^5$ | $10^5$ | 对所有 $4$ 操作,$x=0$。 | | $3$ | $15$ | $10^5$ | $10^5$ | $v=1$ | | $4$ | $15$ | $10^5$ | $10^5$ | 对所有 $4$ 操作,$x$ 是 $u$ 的倍数。 | | $5$ | $15$ | $10^5$ | $10^5$ | 没有 $2,3$ 操作。 | | $6$ | $25$ | $10^5$ | $10^5$ | $/$ |
最新发布
04-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值