建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. selenium-server 启动命令

    启动hub主机: java -jar selenium-server-standalone-2.39.0.jar -role hub 启动node 本地:java -jar selenium-serv ...

  2. idea之映射servlet问题

    方式一: 配置web.xml如下节点,事例如下: servlet>       <servlet-name>HelloWorld</servlet-name>       ...

  3. [ POI 2005 ] Bank Notes

    \(\\\) Description 给出 \(N\) 种货币的面值 \(b_i\) 和个数 \(c_i\) ,求最少需要用多少个硬币凑出 \(Q\) 元钱,并输出任意一种方案. \(n\le 200 ...

  4. CentOS6.6从头到尾部署nginx与tomcat多实例

    前提条件: 1.需要一个全新的centos系统(本文中用到是centos6.6) 2.vmware虚拟机 3.vmware下安装centos系统,以NAT方式与宿主机相连 4.在centos系统中pi ...

  5. 十年后我不会log,还是活的很好啊

    混迹于互联网也一两年了,出于喜爱与生活压力依然会从事很久.迟迟不写博客,大概是觉得知识与经验积累在笔记上时不时看看就好了,而实际情况是笔记很少翻,遇到问题搜博客和百度依然是首选,故开通博客记录自己工作 ...

  6. dutacm.club_1089_A Water Problem_(dp)

    题意:要获得刚好后n个'k'的字符串,有两种操作,1.花费x秒,增加或删除1个'k'; 2.花费y秒,使个数翻倍.问最少需要多少时间获得这个字符串. 思路:i为偶数个'k',dp[i]=min(dp[ ...

  7. MySql的存储过程和触发器

    Mysql的存储过程是类似于其它编程语言中的函数的功能,存储过程内部可以使用顺序循环和转移三种基本程序结构,而且整个存储过程可以接受和返回参数. 创建存储过程(procedure)时,因为其内部有以; ...

  8. static private 与 final 的用法总结

    1.static表示静态.他是属于类的.可以在本身类里直接调用,或在其它类里用类名.方法名调用.不加static表示是实例的方法,必须用实例来调用.在本类里也一样,必须用实例调用 2.private是 ...

  9. TestNG参数化测试

    参数化有两种方法: 第一种:在xml文件中声明 第二种:用@DataProvider注解 先介绍第一种方法: ParameterTest类:用@Parameters({"name" ...

  10. SERE0014: Illegal HTML character: decimal 154

    问题:jmeter,生成报告转化成html,报错SERE0014: Illegal HTML character: decimal 154 原因: 某些字符,特别是控制字符#x7F-#x9F ,在XM ...