建trie树,刚好字符串是反向的,直接在原图上向前搜索就OK了………………

可怜的我竟然用了RK来hash,在test67那里T了……

贴个RK的

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#define LL long long using namespace std; const int MAXN = 10050;
const int MOD = 1e9 + 7;
char tmp[MAXN], str[MAXN]; struct Word{
char s[1005];
int len;
};
int n;
vector<Word>w; int wmod[MAXN * 10];
int smod[1005];
int ymod[1005];
int dp[MAXN]; int main(){
w.clear();
int len;
scanf("%d", &len);
scanf("%s", tmp);
scanf("%d", &n);
ymod[0] = 1;
for(int i = 1; i <= 1000; i++){
ymod[i] = ((LL)ymod[i - 1] * 26) %MOD;
} for(int i = len - 1; i >= 0; i--){
str[len - i] = tmp[i];
}
str[len + 1] = '\0';
int minlen = 0;
Word tmps;
for(int i = 0; i < n; i++){
scanf("%s", tmps.s);
tmps. len = strlen(tmps.s);
w.push_back(tmps);
minlen = max(minlen, tmps.len);
} //cout << 0 << endl; for(int i = 0; i < n; i++){
int val = 0;
int weight = 1;
for(int j = 0; j < w[i].len; j++){
val = ((LL)val + (LL)(w[i].s[j] >= 'A' && w[i].s[j] <= 'Z'? (w[i].s[j] - 'A') : (w[i].s[j] -'a')) * weight % MOD )%MOD;
weight = (LL)weight * 26 % MOD;
}
wmod[i] = val;
}
/*
for(int i = 0; i < n; i++)
cout << wmod[i] << endl;
*/
memset(smod, -1, sizeof(smod));
smod[0] = 0;
memset(dp, -1, sizeof(dp));
dp[0] = 0; for(int i = 1; i <= len; i++){
for(int k = minlen + 1; k >= 1; k--){
if(smod[k - 1] == -1) continue;
else{
smod[k] = ((LL)smod[k- 1] + (LL)ymod[k - 1] *( str[i] - 'a' ))%MOD;
}
}
for(vector<Word>::iterator k = w.begin(); k != w.end(); ++k){
char ss = k->s[k->len - 1];
ss = (ss >= 'A' && ss <= 'Z' )? ss -'A' +'a' : ss;
if(ss != str[i] ) continue;
else {
if(smod[k ->len] != -1 && wmod[k - w.begin()] == smod[k->len] && dp[i - k->len] >= 0){
dp[i] = k - w.begin();
break;
}
}
}
}
int i = len;
while(i){
printf("%s", w[dp[i]].s);
i = i - w[dp[i]].len;
if(i) printf(" ");
}
printf("\n"); }

  

trie树简便快捷啊……

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
using namespace std; const int maxn = 1E4 + 10; struct T{
int ch[26];
int END;
T () {
memset(ch,0,sizeof(ch));
END = 0;
}
}t[1000010]; int cnt,n,m,f[maxn],last[maxn];
char x[maxn],y[1010]; vector <char> v[maxn*10]; void Insert(int o,int pos,int len,int num)
{
for (; pos < len; pos++) {
int to;
if (y[pos] >= 'a') to = y[pos]-'a';
else to = y[pos]-'A';
if (!t[o].ch[to]) t[o].ch[to] = ++cnt;
o = t[o].ch[to];
}
t[o].END = num;
} void solve(int pos)
{
int END = max(1,pos-999);
int o = 0;
for (int i = pos; i >= END; i--) {
o = t[o].ch[x[i]-'a'];
if (!o) break;
if ((f[i-1] || i == 1) && t[o].END) {
last[pos] = i;
f[pos] = t[o].END;
break;
}
}
} void pri(int pos)
{
if (last[pos] != 1) pri(last[pos]-1);
int len = v[f[pos]].size();
for (int i = 0; i < len; i++) printf("%c",v[f[pos]][i]);
printf(" ");
} int main()
{
#ifdef YZY
freopen("yzy.txt","r",stdin);
#endif cin >> n;
scanf("%s",1+x);
cin >> m;
for (int i = 1; i <= m; i++) {
scanf("%s",&y);
int len = strlen(y);
Insert(0,0,len,i);
for (int j = 0; j < len; j++) v[i].push_back(y[j]);
} for (int i = 1; i <= n; i++)
solve(i);
pri(n);
return 0;
}

  

Manthan, Codefest 16 C的更多相关文章

  1. Manthan, Codefest 16 D. Fibonacci-ish

    D. Fibonacci-ish time limit per test 3 seconds memory limit per test 512 megabytes input standard in ...

  2. Manthan, Codefest 16(B--A Trivial Problem)

    B. A Trivial Problem time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  3. Manthan, Codefest 16 -C. Spy Syndrome 2

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  4. Manthan, Codefest 16 -A Ebony and Ivory

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  5. Manthan, Codefest 16

    暴力 A - Ebony and Ivory import java.util.*; import java.io.*; public class Main { public static void ...

  6. CF Manthan, Codefest 16 G. Yash And Trees 线段树+bitset

    题目链接:http://codeforces.com/problemset/problem/633/G 大意是一棵树两种操作,第一种是某一节点子树所有值+v,第二种问子树中节点模m出现了多少种m以内的 ...

  7. CF #Manthan, Codefest 16 C. Spy Syndrome 2 Trie

    题目链接:http://codeforces.com/problemset/problem/633/C 大意就是给个字典和一个字符串,求一个用字典中的单词恰好构成字符串的匹配. 比赛的时候是用AC自动 ...

  8. CF Manthan, Codefest 16 B. A Trivial Problem

    数学技巧真有趣,看出规律就很简单了 wa 题意:给出数k  输出所有阶乘尾数有k个0的数 这题来来回回看了两三遍, 想的方法总觉得会T 后来想想  阶乘 emmm  1*2*3*4*5*6*7*8*9 ...

  9. Manthan, Codefest 16 H. Fibonacci-ish II 大力出奇迹 莫队 线段树 矩阵

    H. Fibonacci-ish II 题目连接: http://codeforces.com/contest/633/problem/H Description Yash is finally ti ...

  10. Manthan, Codefest 16 E. Startup Funding ST表 二分 数学

    E. Startup Funding 题目连接: http://codeforces.com/contest/633/problem/E Description An e-commerce start ...

随机推荐

  1. 1、DOS基本命令

    命令dir能给列出当前目录下面的所有文件.程序和子目录.所有目录(Windows 中称为文件夹)的目录名前面都有一个<DIR>标记.文件和程序名前面显示有这些文件和程序的大小. 想说的是, ...

  2. JSON基础 JS操作JSON总结

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScript 原生格式,这意 ...

  3. 【工具】Webpack

    远程仓库建立 码云创建组织项目 git clone ssh 切换到主分支mmall-fe后git remote add origin ssh git pull origin master把master ...

  4. ImmutableJS

    引用大神的一句话:(具体是谁自己问度娘) Shared mutable state is the root of all evil(共享的可变状态是万恶之源) -- Pete Hunt   JavaS ...

  5. JPQL 模糊查询,查询条件拼接(like使用)

    @Transactional public List<ViewCorplist2> findAllCorpsLikeK(String kw) { System.out.println(kw ...

  6. docker在ubuntu16.04下的安装及阿里云镜像的配置

    1.获取最新版本的 Docker 安装包 anmin@ubuntu:~$ wget -qO- https://get.docker.com/ | sh 安装完成后有个提示: If you would ...

  7. HDU多校Round 4

    Solved:3 rank:405................................. B. Harvest of Apples 知道了S(n,m) 可以o(1)的求S(n - 1, m ...

  8. BZOJ5314: [Jsoi2018]潜入行动 (树形DP)

    题意:一棵树选择恰好k个结点放置监听器 每个监听器只能监听相邻的节点 问能使得所有节点被监听的种类数 题解:反正就是很well-known的树形DP了 至于时间复杂度为什么是nk 不会不学 很好想到四 ...

  9. wpf 界面加载 Command

    导入 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" <i:Interaction. ...

  10. iOS多媒体框架介绍

    媒体层 媒体层包含图形技术.音频技术和视频技术,这些技术相互结合就可为移动设备带来最好的多媒体体验,更重要的是,它们让创建外观音效俱佳的应用程序变得更加容易.您可以使用iOS的高级框架更快速地创建高级 ...