建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. ADB Usage Complete / ADB 用法大全

    ADB,即 Android Debug Bridge,它是 Android 开发/测试人员不可替代的强大工具,也是 Android 设备玩家的好玩具. 持续更新中,欢迎提 PR 和 Issue 补充指 ...

  2. 四种IO模型

    四种 IO 模型:       首先需要明确,IO发生在 用户进程 与 操作系统 之间.可以是客户端IO也可以是服务器端IO. 阻塞IO(blocking IO):     在linux中,默认情况下 ...

  3. Java编程思想读书笔记_第6章(访问权限)

    四种访问权限: public private 包访问权限 protected 如果没有明确指定package,则属于默认包 package access.dessert; public class C ...

  4. 2017-12-04HTML table布局

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  5. css样式获取及兼容性(原生js)

    类选择器兼容性 getbyclass()类选择器,在IE8及以下均不可用. // 类选择器的兼容性 function getbyclass(parentName,Name){ var parentNa ...

  6. Mysql5.7多源复制,过滤复制一段时间后增加复制一个库的实现方法

    多源复制如果是整个实例级别的复制,那不存在下面描述的情况. 如果是对其中一个或多个主实例都是过滤复制,并且运行一段时间后,想在这个源上再增加一个库怎么实现?   主1:192.168.1.10 330 ...

  7. Mysql导入导出大量数据的方法、备份恢复办法

    经常使用PHP+Mysql的朋友一般都是通过phpmyadmin来管理数据库的.日常的一些调试开发工作,使用phpmyadmin确实很方便.但是当我们需要导出几百兆甚至几个G的数据库时,phpmyad ...

  8. HDU_1561_The more, The Better_树型dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1561 The more, The Better Time Limit: 6000/2000 MS (J ...

  9. sql的for update

    欢迎大家吐槽 oracle行级共享锁 通常是通过select … from for update语句添加的,同时该方法也是我们用来手工锁定某些记录的主要方法.比如,当我们在查询某些记录的过程中,不希望 ...

  10. logging,numpy,pandas,matplotlib模块

    logging模块 日志总共分为以下五个级别,这五个级别自下而上进行匹配debug->info->warning->error->critical,默认的最低级别warning ...