Manthan, Codefest 16 C
建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的更多相关文章
- Manthan, Codefest 16 D. Fibonacci-ish
D. Fibonacci-ish time limit per test 3 seconds memory limit per test 512 megabytes input standard in ...
- 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 ...
- Manthan, Codefest 16 -C. Spy Syndrome 2
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- Manthan, Codefest 16 -A Ebony and Ivory
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- Manthan, Codefest 16
暴力 A - Ebony and Ivory import java.util.*; import java.io.*; public class Main { public static void ...
- CF Manthan, Codefest 16 G. Yash And Trees 线段树+bitset
题目链接:http://codeforces.com/problemset/problem/633/G 大意是一棵树两种操作,第一种是某一节点子树所有值+v,第二种问子树中节点模m出现了多少种m以内的 ...
- CF #Manthan, Codefest 16 C. Spy Syndrome 2 Trie
题目链接:http://codeforces.com/problemset/problem/633/C 大意就是给个字典和一个字符串,求一个用字典中的单词恰好构成字符串的匹配. 比赛的时候是用AC自动 ...
- CF Manthan, Codefest 16 B. A Trivial Problem
数学技巧真有趣,看出规律就很简单了 wa 题意:给出数k 输出所有阶乘尾数有k个0的数 这题来来回回看了两三遍, 想的方法总觉得会T 后来想想 阶乘 emmm 1*2*3*4*5*6*7*8*9 ...
- Manthan, Codefest 16 H. Fibonacci-ish II 大力出奇迹 莫队 线段树 矩阵
H. Fibonacci-ish II 题目连接: http://codeforces.com/contest/633/problem/H Description Yash is finally ti ...
- Manthan, Codefest 16 E. Startup Funding ST表 二分 数学
E. Startup Funding 题目连接: http://codeforces.com/contest/633/problem/E Description An e-commerce start ...
随机推荐
- [ CodeForces 1059 D ] Nature Reserve
\(\\\) \(Description\) 你现在有\(N\)个分布在二维平面上的整点\((x_i,y_i)\),现在需要你找到一个圆,满足: 能够覆盖所有的给出点 与\(x\)轴相切 现在需要你确 ...
- 前端--2、CSS基础
CSS的部分: CSS四种类引入方式(了解) style的定义原则: 基本选择器 示例: 层级选择器 组合选择器 后代选择器 ★ 子代选择器 毗邻选择器 普通兄弟选择器 “与”选择器 ★ “或”选择器 ...
- 全志A33平台编译linux(分色排版)sina33
全志A33平台编译linux 大文实验室/大文哥 壹捌陆捌零陆捌捌陆捌贰 21504965 AT qq.com 完成时间:2017/12/12 17:36 版本:V1.0 Xshell 5 (Buil ...
- python自动化--语言基础四模块、文件读写、异常
模块1.什么是模块?可以理解为一个py文件其实就是一个模块.比如xiami.py就是一个模块,想引入使用就在代码里写import xiami即可2.模块首先从当前目录查询,如果没有再按path顺序逐一 ...
- DataWhale学习计划(第六期):python基础任务6
file-settings 然后选择project Interpreter 把project Interpreter复选框里面的地址换成你安装的anaconda下的python.exe的地址,点击sh ...
- JS模拟CSS3动画-贝塞尔曲线
一.什么是贝塞尔曲线 1962年,法国工程师皮埃尔·贝塞尔(Pierre Bézier),贝塞尔曲线来为为解决汽车的主体的设计问题而发明了贝塞尔曲线.如今,贝赛尔曲线是计算机图形学中相当重要的一种曲线 ...
- java学习_5_24
TreeMap底层是用红黑树实现的,源码如下: /** * A Red-Black tree based {@link NavigableMap} implementation. * The map ...
- 基础:VS快捷键
VS.net中快捷键收缩和展开代码段 i. Ctrl-M-O 折叠所有方法 ii. Ctrl-M-P 展开所有方法并停止大纲显示(不可以再折叠了) iii. Ctrl-M-M 折叠或展开当 ...
- jQuery图片延迟加载插件jQuery.lazyload 的使用
使用方法 引用jquery和jquery.lazyload.js到你的页面 1 2 <script src="jquery-1.11.0.min.js"></sc ...
- anaconda镜像
下载一个包老是下载不下来,于是放弃了官方版改为国内镜像. 清华镜像 (打开Anaconda Prompt 或者 打开cmd,运行下面的命令) conda config --add channels ...