3.3 字符串(1)

Trie 前缀树 字典树

例题11  LA 3942 http://acm.hust.edu.cn/vjudge/problem/22109

字典树第一个例题我用set水过,先记录这个版本吧,题意是,给一个串,还有一些短串的集合,问有多少种不同的拆分方法使得拆分完后,每个子串都在集合中。

dp[len] 表示前len个有多少拆分法 dp[0]=1, 前0个字符有一种拆分法,空嘛。  dp[len] 就是答案,  转移就是, 如果接下来一段在集合中,那么就可以转移到长度加这段的长度。

起点有la=3e5,,对每一个起点,加的串可能有lbi=100,都要枚举,接下来就是判断这个串是否存在,用hash值判,o1,set查询logn ,n=4000, 总体复杂度 la*lb*logn 飘过。

 1 //#define txtout
 2 //#define debug
 3 #include<bits/stdc++.h>
 4 #define mt(a,b) memset(a,b,sizeof(a))
 5 using namespace std;
 6 typedef long long LL;
 7 const double pi=acos(-1.0);
 8 const double eps=1e-8;
 9 const int inf=0x3f3f3f3f;
10 const int M=3e5+10;
11 const LL MOD=20071027;
12 class String_Hash {///字符串哈希 init O(n) query O(1)
13     typedef unsigned long long typec;///hash 值类型
14     static const int MV=3e5+10;///串长度
15     static const int key=137;///hash 种子值选素数好
16     typec H[MV],xp[MV];
17 public:
18     void init(char s[],int ls) { ///传入串数组和串长
19         H[ls]=0;
20         for(int i=ls-1; i>=0; i--) {
21             H[i]=H[i+1]*key+s[i];
22         }
23         xp[0]=1;
24         for(int i=1; i<=ls; i++) {
25             xp[i]=xp[i-1]*key;
26         }
27     }
28     typec get(int pos,int len) { ///传入初始位置 pos,串长 len,返回串 hash 值
29         return H[pos]-H[pos+len]*xp[len];
30     }
31 }Hash;
32 int n;
33 char a[M];
34 char b[4010][110];
35 LL dp[M];
36 set<unsigned long long> s;
37 int max_len;
38 void init_set() {
39     s.clear();
40     max_len=0;
41     for(int i=0; i<n; i++) {
42         int lb=strlen(b[i]);
43         Hash.init(b[i],lb);
44         s.insert(Hash.get(0,lb));
45         max_len=max(max_len,lb);
46     }
47 }
48 LL solve() {
49     init_set();
50     int len=strlen(a);
51     for(int i=0; i<=len; i++) {
52         dp[i]=0;
53     }
54     dp[0]=1;
55     Hash.init(a,strlen(a));
56     for(int i=0; i<len; i++) {
57         if(dp[i]==0) continue;
58         for(int j=0; j<max_len; j++) {
59             if(!s.count(Hash.get(i,j+1))) continue;
60             dp[i+j+1]+=dp[i];
61             dp[i+j+1]%=MOD;
62         }
63     }
64     return dp[len];
65 }
66 int main() {
67 #ifdef txtout
68     freopen("in.txt","r",stdin);
69     freopen("out.txt","w",stdout);
70 #endif // txtout
71     int cas=1;
72     while(~scanf("%s%d",a,&n)) {
73         for(int i=0; i<n; i++) {
74             scanf("%s",b[i]);
75         }
76         printf("Case %d: %lld\n",cas++,solve());
77     }
78     return 0;
79 }

end

时间: 2024-08-10 00:07:45

3.3 字符串(1)的相关文章

条件、循环、函数定义、字符串操作练习

注意标准库的两种导入与使用方式,建议大家采用<库名>.<函数名>的方式. 对前面的代码进行优化,用for,while,if,def实现: 用循环画五角星 1 import turtle 2 3 turtle.fillcolor("red") 4 turtle.begin_fill() 5 for i in range(5): 6 turtle.forward(100) 7 turtle.right(144) 8 turtle.end_fill() 用循环画同心圆

sql常用格式化函数及字符串函数

一.常用格式化函数 1.日期转字符串 select to_char(current_timestamp, 'YYYY-MM-DD HH24:MI:SS') //2017-09-18 22:41:50 YYYY:年(4和更多位) MM:月份号(01-12) DD:一个月里的日(01-31) HH24:一天的小时数(00-23) MI:分钟(00-59) SS:秒(00-59) 2.字符串转日期 select to_date('2017-09-18','YYYY-MM-DD') //2017-09-

PHP 格式化字符串sprintf()

字符串函数 sprintf() 函数把格式化的字符串写入一个变量中 函数说明:sprintf(格式, 要转换的字符串)  参考PHP手册 返回: 格式化后的字符串 举例: 如:保留2位小数, $str = '99.9';echo sprintf('%01.2f', $str);结果为:99.90 echo round($str, 2); 结果为:99.9

js中字符串的替换

定义和用法 replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 语法 stringObject.replace(regexp/substr,replacement)参数 描述 regexp/substr 必需.规定子字符串或要替换的模式的 RegExp 对象. 请注意,如果该值是一个字符串,则将它作为要检索的直接量文本模式,而不是首先被转换为 RegExp 对象. replacement 必需.一个字符串值.规定了替换文本或生成替换文本的函数. 返

数组、字符串、集合

数组与集合的转换.数组与字符串的转换 ========数组变集合 String[] arr = {"abc","cc","kkkk"}; //把数组变成list集合有什么好处? /* 可以使用集合的思想和方法来操作数组中的元素. 注意:将数组变成集合,不可以使用集合的增删方法. 因为数组的长度是固定. contains. get indexOf() subList(); 如果你增删.那么会产生UnsupportedOperationExcepti

《Java编程思想》第十三章 字符串

<Java编程思想>读书笔记 1.String作为方法的参数时,会复制一份引用,而该引用所指的对象其实一直待在单一的物理位置,从未动过. 2.显式地创建StringBuilder允许预先为他指定大小.如果知道字符串多长,可以预先指定StringBuilder的大小避免多次重新分配的冲突. 1 /** 2 * @author zlz099: 3 * @version 创建时间:2017年9月1日 下午4:03:59 4 */ 5 public class UsingStringBuilder {

SpringMVC后台使用对象接受参数字符串转日期

在springMVC配置文件中加入: <bean id="dateConvert" class="com.iomp.util.DateConvert"/> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property

python判断字符串,str函数isdigit、isdecimal、isnumeric的区别

s为字符串s.isalnum() 所有字符都是数字或者字母s.isalpha() 所有字符都是字母s.isdigit() 所有字符都是数字s.islower() 所有字符都是小写s.isupper() 所有字符都是大写s.istitle() 所有单词都是首字母大写,像标题s.isspace() 所有字符都是空白字符.\t.\n.\r 判断是整数还是浮点数a=123b=123.123 >>>isinstance(a,int)True>>>isinstance(b,floa

【自动化__持续集成】___java___字符串转数组

一.代码如下 package www.wujianbotwo; public class Demo08 { public static void main(String[] args) { // TODO Auto-generated method stub String content= "wujianbo,wujianob,33333"; String[] ud= content.split(",");//将字符串分段成一个数组 for(int i=0; i&l

字符串的模式匹配中的算法

字符串的模式匹配是一个比较经典的问题:假设有一个字符串S,称其为主串,然后还有一个字符串T,称其为子串. 现在要做的是,从主串S当中查找子串T的位置,如果存在返回位置值,如果不存在返回-1.另外主串又称为目标串, 子串称为模式串. 暴力匹配算法 这是一个经典的串匹配问题,涉及的算法也比较多,先讨论第一种简单的暴力算法,思路如下 将主串S的第pos个字符 与 子串T的第一个字符比较, 若相同,继续比较子串和主串后面的字符. 若不相同,那么从主串S的第(pos + 1)个字符开始继续向后匹配,直到匹