题目原文:
Accept: 47 Submit: 152
Time Limit: 2000 mSec Memory Limit : 262144 KB
Problem Description
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
Sample Output
Source
第八届福建省大学生程序设计竞赛-重现赛(感谢承办方厦门理工学院)解题思路:预处理所有字符串从后向前的hash值,然后针对每次查询,O(n)比较。
AC代码:
/*
@Author: wchhlbt
@Date: 2017/7/24
*/
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <limits>
#include <climits>
#include <cstdio>
#define Fori(x) for(int i=0;i<x;i++)
#define Forj(x) for(int j=0;j<x;j++)
#define maxn 300007
#define inf 0x3f3f3f3f
#define pb push_back
#define ONES(x) __builtin_popcount(x)
#define _ << " " <<
using namespace std;
typedef long long ll ;
const double eps =1e-8;
const int mod = 1000000007;
typedef pair<int, int> P;
const double PI = acos(-1.0);
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
/*
* 把字符串变成X进制数,可以完成$ O(1) $比较 ,
* 调用 Init() 初始化幂 ,
* 调用 Init(u64* Hash, int len) 初始化 Hash 数组 ,
* Get(u64* Hash, int p, int L) 表示获得以 p 开头长度为 L 的字符串 Hash 。
* Base 需要选用质数
*/
typedef unsigned long long u64;
const u64 Base = 31;
const int N = 2007;
char s[N][N];
u64 Hash[N][N];
u64 Pow[N];
void Init() {
Pow[0] = 1;
for(int i = 1; i < N; i++) Pow[i] = Pow[i - 1] * Base;
}
void Init(int len, int id) {
Hash[id][len] = 0;
for(int i = len - 1; i >= 0; i--)
Hash[id][i] = (u64)Hash[id][i + 1] * Base + (s[id][i] - 'a' + 1);
}
u64 Get(int p, int L, int id) {
return Hash[id][p] - Hash[id][p + L] * Pow[L];
}
int w[N];
int len[N];
int main()
{
int t;
scanf("%d",&t);
Init();
while(t--){
int n;
scanf("%d",&n);
for(int i = 1; i<=n; i++){
scanf("%s%d",&s[i],&w[i]);
len[i] = strlen(s[i]);
Init(len[i], i);
}
int q;
scanf("%d",&q);
while(q--){
int type;
scanf("%d",&type);
if(type==1){
int x,y;
scanf("%d%d",&x,&y);
w[x] = y;
}
else{
int x;
scanf("%d",&x);
int ans = 0;
for(int i = 1; i<=n; i++){
if(len[i]>=len[x] && w[i]<=w[x] && Hash[i][len[i]-len[x]]==Hash[x][0]){
ans++;
}
}
printf("%d\n", ans);
}
}
}
return 0;
}