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 ...
随机推荐
- [转]TFS下的源代码控制
本文转自:http://www.cnblogs.com/ajiefj/archive/2010/04/23/1718450.html 以下主要描述了: TFS源代码控制系统的基本场景 如何把一个项目添 ...
- NHibernate3.2学习笔记-几种查询方式
一.开发环境 数据库:SQLServer2008 编译器:VS2010 .Net版本:.Net Framework 4.0 二.开发过程 1.项目结构 承接上一篇 2.执行sql语句 (1)基本语法 ...
- 网上商城 Incorrect datetime value: '' for column 'ordertime' at row 1
今天在做商城项目的[提交订单]功能的时候,向数据库插入数据报错:Incorrect datetime value: '' for column 'ordertime' at row 1 public ...
- 一个JavaScript贷款计算器
通过本案例,将会学到: . 如何在文档中查找元素 . 如何通过表单input元素来获取用户的输入数据 . 如何通过文档元素来设置HTML内容 . 如何将数据存储在浏览器中 . 如何使用脚本发起HTTP ...
- javascript 到将来某个时间(2020-5-20)的倒计时
function countDown(dateStr){ var end = +new Date(dateStr), start = +new Date(), during = Math.floor( ...
- python--12、pymysql模块
安装 pip3 install pymysql 使用方法(示例表为用户注册信息表): import pymysql user=input('username: ').strip() pwd=input ...
- Android PopupWindow使用时注意
项目中使用PopupWindown出现的坑 1.部分设备,PopWindow在Android4.0后版本,出现NullPointerException调用以下方法可解决, fixPopupWindow ...
- 联想 K5 Pro(L38041)免解锁BL 免rec 保留数据 ROOT Magisk Xposed 救砖 ZUI 5.0.188
>>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...
- PHP引用(&)的考察点
引用的概念 在PHP中引用意味着用不同的名字访问同一个变量内容. 定义方式 使用 & 符号来表示 变量的引用 $a = 'ABC'; //开辟一块内存空间存储数据,$a指向该空间 $b = & ...
- 【转】WITH AS 用法
转载自:http://blog.csdn.net/shaochao14/article/details/6223052 一.WITH AS的含义 WITH AS短语,也叫做子查询部分(subquery ...