Kim is a magician, he can use n kinds of magic, number from 1 to n. We use string Si to describe magic i. Magic Si will make Wi points of damage. Note that Wi may change over time.

Kim obey the following rules to use magic:

Each turn, he picks out one magic, suppose that is magic Sk, then Kim will use all the magic i satisfying the following condition:

1. Wi<=Wk

2. Sk is a suffix of Si.

Now Kim wondering how many magic will he use each turn.

Note that all the strings are considered as a suffix of itself.

Input

First line the number of test case T. (T<=6)

For each case, first line an integer n (1<=n<=1000) stand for the number of magic.

Next n lines, each line a string Si (Length of Si<=1000) and an integer Wi (1<=Wi<=1000), stand for magic i and it’s damage Wi.

Next line an integer Q (1<=Q<=80000), stand for there are Q operations. There are two kinds of operation.

“1 x y” means Wx is changed to y.

“2 x” means Kim has picked out magic x, and you should tell him how many magic he will use in this turn.

Note that different Si can be the same.

Output

For each query, output the answer.

Sample Input

1
5
abracadabra 2
adbra 1
bra 3
abr 3
br 2
5
2 3
2 5
1 2 5
2 3
2 2

Sample Output

3
1
2
1

题意:

给n个长度<=1000的字符串S[i](仅由小写字母构成),每个字符串有权值w[i]
有两种操作
2 k 选中第k个字符串,问你有多少个字符串满足w[i]<=w[k]而且S[k]是S[i]的后缀
1 k y 把第k个字符串的权值修改为

暴力HASH 预处理下后缀

然后暴力查询

 #include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define fuck(x) cout<<"["<<x<<"]"<<endl
#define f(a) a*a
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pf printf
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define FIN freopen("DATA.txt","r",stdin)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) x&-x
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 0x7fffffff;
const int mod = 1e9 + ;
const int maxn = 1e3 + ;
int t, n, m, val[maxn], len[maxn];
char mp[maxn][maxn];
vector<int>cnt[maxn];
ULL HA[maxn][maxn], p[maxn], seed = ;
ULL getcnt(int id, int l, int r) {
return HA[id][r] - HA[id][l - ] * p[r - l + ];
}
void init() {
p[] = ;
for (int i = ; i < maxn ; i++) p[i] = p[i - ] * seed;
for (int i = ; i <= n ; i++) {
HA[i][] = ;
for (int j = ; j <= len[i] ; j++) {
HA[i][j] = HA[i][j - ] * seed + mp[i][len[i] - j + ];
}
}
for (int i = ; i <= n ; i++) {
cnt[i].push_back(i);
for (int j = i + ; j <= n ; j++ ) {
if (len[i] == len[j] && HA[i][len[i]] == HA[j][len[j]]) cnt[i].push_back(j), cnt[j].push_back(i);
else if (len[i] > len[j] && HA[j][len[j]] == getcnt(i, , len[j] )) cnt[j].push_back(i);
else if (len[i] < len[j] && HA[i][len[i]] == getcnt(j, , len[i] )) cnt[i].push_back(j);
}
}
}
int main() {
//FIN;
sf(t);
while(t--) {
sf(n);
for (int i = ; i <= n ; i++) {
cnt[i].clear();
scanf("%s%d", mp[i] + , &val[i]);
len[i] = strlen(mp[i] + );
}
init();
sf(m);
while(m--) {
int op, x, y;
sf(op);
if (op == ) {
sff(x, y);
val[x] = y;
} else {
sf(x);
int ans = ;
for (int i = ; i < cnt[x].size() ; i++)
if (val[cnt[x][i]] <= val[x]) ans++;
printf("%d\n", ans);
}
}
}
return ;
}
												

Magic FZU - 2280 无脑HASH暴力的更多相关文章

  1. I - Magic FZU - 2280 (字符串hash)

    题目链接: I - Magic FZU - 2280 学习链接: FZU - 2280 I - Magic 题目大意: 给你nn个字符串,每个字符串有一个值ww,有qq次询问,一共两种操作:一是“1, ...

  2. 【原】无脑操作:express + MySQL 实现CRUD

    基于node.js的web开发框架express简单方便,很多项目中都在使用.这里结合MySQL数据库,实现最简单的CRUD操作. 开发环境: IDE:WebStorm DB:MySQL ------ ...

  3. Go语言及Web框架Beego环境无脑搭建

    [原]Go语言及Web框架Beego环境无脑搭建 本文涉及软件均以截至到2013年10月12日的最新版本为准 1. 相关软件准备: 1) go1.2rc1.windows-386.msi,对应32位w ...

  4. 【原】无脑操作:eclipse + maven搭建SSM框架

    网上看到一些Spring + Spring MVC + MyBatis框架的搭建教程,不是很详细或是时间久远了,自己动手整一个简单无脑的! 0.系统环境 1)Windows 10 企业版 2)JDK ...

  5. 计蒜客 无脑博士 bfs

    题目链接无脑博士的试管们 思路:直接模拟倒水过程即可,但是需要记忆判断当前的情况是否已经处理过.dfs和bfs都ok AC代码 #include <cstdio> #include < ...

  6. CodeForces 909E Coprocessor(无脑拓扑排序)

    You are given a program you want to execute as a set of tasks organized in a dependency graph. The d ...

  7. 【原】无脑操作:ElasticSearch学习笔记(01)

    开篇来自于经典的“保安的哲学三问”(你是谁,在哪儿,要干嘛) 问题一.ElasticSearch是什么?有什么用处? 答:截至2018年12月28日,从ElasticSearch官网(https:// ...

  8. 【原】无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础授权权限

    上一篇<[原]无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础认证权限>介绍了实现Shiro的基础认证.本篇谈谈实现 ...

  9. 【原】无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础认证权限

    开发环境搭建参见<[原]无脑操作:IDEA + maven + SpringBoot + JPA + Thymeleaf实现CRUD及分页> 需求: ① 除了登录页面,在地址栏直接访问其他 ...

随机推荐

  1. 【template、import、include】微信小程序:模板(template)、引用(import、include)说明

    模板(template): 定义模板 <template name="[String]"> <!-- 模板代码 --> <view> <t ...

  2. lintcode172 删除元素

    删除元素   给定一个数组和一个值,在原地删除与值相同的数字,返回新数组的长度. 元素的顺序可以改变,并且对新的数组不会有影响. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一个数组 [0, ...

  3. 181. Flip Bits【LintCode, by java】

    Description Determine the number of bits required to flip if you want to convert integer n to intege ...

  4. [SHELL]输出目录下所有的可执行文件,批量创建用户

    #!/bin/bash IFS=: for folder in $PATH #PATH变量分隔符为: do echo $folder echo ------------------ for file ...

  5. 《Effective C++》读书笔记 资源管理

    C++程序中最常用的资源包括动态分配的内存,文件描述器,互斥锁,数据库连接,网络socket等等.不论哪种资源,重要的是,当你不再使用他时,必须将他归还给系统. 一个很好的做法是以对象管理资源.把资源 ...

  6. 一键部署 Docker Datacenter ---简化docker数据中心安装步骤

    DDC 简介 2016年2月下旬,Docker发布了企业级容器管理和服务部署的整体解决方案平台-Docker Datacenter,简称DDC.DDC 有三个组件构成:1. Docker Univer ...

  7. 基础数据类型-list

    序列是python中的基础数据结构,序列里每一个元素都有一个下标,从0开始,依次递增. list,tuple,dictionary是使用最频繁的三类数据结构. (1)序列都有的方法包括:索引,切片,检 ...

  8. Java中 Auto-boxing/unboxing

    Java 中 Auto-boxing/unboxing 机制,在合适的时机自动打包,解包. 1. 自动将基础类型转换为对象: 2. 自动将对象转换为基础类型: Demo_1: import java. ...

  9. java — 设计模式

    设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性. 一.设计模式的分类 ...

  10. LintCode-54.转换字符串到整数

    转换字符串到整数 实现atoi这个函数,将一个字符串转换为整数.如果没有合法的整数,返回0.如果整数超出了32位整数的范围,返回INT_MAX(2147483647)如果是正整数,或者INT_MIN( ...