Hash练习题:【Codeforce round #166 】 D. Good Substrings

来源:http://codeforces.com/contest/271/problem/D

D. Good Substrings
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.

A substring s[l...r] (1 ≤ l ≤ r ≤ |s|) of string s  =  s1s2...s|s| (where |s| is the length of string s) is string slsl + 1...sr.

The substring s[l...r] is good, if among the letters sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).

Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] ≠ s[p...q].

Input

The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters.

The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on.

The third line of the input consists a single integer k (0 ≤ k ≤ |s|) — the maximum acceptable number of bad characters in a good substring.

Output

Print a single integer — the number of distinct good substrings of string s.

Sample test(s)
input
ababab
01000000000000000000000000
1
output
5
input
acbacbacaa
00000000000000000000000000
2
output
8
Note

In the first example there are following good substrings: "a", "ab", "b", "ba", "bab".

In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".


<think> 我们被要求提供一个完整的shardingsphere.yaml文件示例及配置说明。根据引用内容,特别是引用[2]中提到的配置要点,我们将构建一个示例配置。 注意:由于引用中提到了版本控制(如v2.3),我们将基于较新版本的ShardingSphere(例如5.x)的配置结构来编写,因为旧版本(如引用[3]提到的)可能有所不同,但用户要求的是完整正确的示例,所以采用新版本的格式。 根据引用[2],配置要点包括: - 模块化配置:数据源、规则、属性分离 - 算法复用:命名算法多处引用 - 环境适配:通过变量区分环境 - 规则组合:灵活叠加多种数据治理能力 - 动态治理:结合DistSQL实现配置热更新 同时,引用[2]也给出了一些配置片段,我们可以参考。 下面是一个完整的shardingsphere.yaml示例,并附有详细说明。 </think> 以下是一个完整的 ShardingSphere YAML 配置文件示例及详细说明,基于 Apache ShardingSphere 5.x 版本: ```yaml # ShardingSphere YAML 完整配置示例 version: 5.3.2 # 配置版本标识 changelog: # 变更历史记录 - version: 5.2.0 date: 2023-05-15 comment: 新增数据加密规则 # 数据源配置 (必填) dataSources: ds_0: # 数据源名称 dataSourceClassName: com.zaxxer.hikari.HikariDataSource url: jdbc:mysql://127.0.0.1:3306/db0?serverTimezone=UTC username: root password: root ds_1: dataSourceClassName: com.zaxxer.hikari.HikariDataSource url: jdbc:mysql://127.0.0.1:3306/db1?serverTimezone=UTC username: root password: root # 规则配置 (核心部分) rules: # 分片规则配置 - !SHARDING tables: t_order: # 逻辑表名 actualDataNodes: ds_${0..1}.t_order_${0..1} # 物理表分布 tableStrategy: # 分表策略 standard: shardingColumn: order_id shardingAlgorithmName: t_order_inline keyGenerateStrategy: # 分布式ID生成 column: order_id keyGeneratorName: snowflake # 分片算法定义 shardingAlgorithms: t_order_inline: type: INLINE props: algorithm-expression: t_order_${order_id % 2} # 分布式序列算法 keyGenerators: snowflake: type: SNOWFLAKE props: worker-id: 123 # 读写分离规则 - !READWRITE_SPLITTING dataSources: pr_ds: # 读写分离逻辑数据源 writeDataSourceName: ds_0 readDataSourceNames: - ds_1 loadBalancerName: round_robin loadBalancers: round_robin: type: ROUND_ROBIN # 数据加密规则 - !ENCRYPT encryptors: aes_encryptor: type: AES props: aes-key-value: 123456abc tables: t_user: columns: phone: cipherColumn: phone_cipher encryptorName: aes_encryptor # 全局属性配置 props: sql-show: true # 打印SQL日志 sql-simple: false kernel-executor-size: 20 # 工作线程数 check-table-metadata-enabled: false # 启动时检查表元数据 ``` ### 配置详解 #### 1. **版本控制** ```yaml version: 5.3.2 changelog: - version: 5.2.0 date: 2023-05-15 comment: 新增数据加密规则 ``` - `version`: 声明配置版本,需与 ShardingSphere 版本兼容[^2] - `changelog`: 记录配置变更历史,便于团队协作维护 #### 2. **数据源配置** ```yaml dataSources: ds_0: dataSourceClassName: com.zaxxer.hikari.HikariDataSource url: jdbc:mysql://127.0.0.1:3306/db0 ``` - 支持多数据源定义,每个数据源需指定连接池实现类(如 HikariCP、Druid) - 支持表达式变量 `${env}` 实现环境适配(开发/测试/生产) #### 3. **核心规则配置** **分片规则** ```yaml tables: t_order: actualDataNodes: ds_${0..1}.t_order_${0..1} # 物理节点表达式 tableStrategy: standard: shardingColumn: order_id # 分片键 shardingAlgorithmName: t_order_inline # 引用算法 ``` - `actualDataNodes`: 使用 Groovy 表达式定义物理表分布 - 支持数据库分片(databaseStrategy)和表分片(tableStrategy)双维度 **算法复用** ```yaml shardingAlgorithms: t_order_inline: type: INLINE # 内置行表达式算法 props: algorithm-expression: t_order_${order_id % 2} # 分片逻辑 ``` - 支持 10+ 种内置算法:HASH_MOD、VOLUME_RANGE 等 - 命名算法可被多表引用避免重复配置[^1] **分布式主键** ```yaml keyGenerators: snowflake: type: SNOWFLAKE # 雪花算法 props: worker-id: 123 # 工作节点ID ``` #### 4. **读写分离** ```yaml - !READWRITE_SPLITTING dataSources: pr_ds: writeDataSourceName: ds_0 # 写数据源 readDataSourceNames: - ds_1 # 读数据源(可多个) loadBalancerName: round_robin # 负载均衡策略 ``` - 支持静态分组和动态发现(需集成注册中心) - 负载均衡策略:ROUND_ROBIN(轮询)、RANDOM(随机)等 #### 5. **数据加密** ```yaml - !ENCRYPT tables: t_user: columns: phone: cipherColumn: phone_cipher # 密文字段 encryptorName: aes_encryptor ``` - 支持 AES、MD5 等加密算法 - 自动处理明文/密文数据转换 #### 6. **全局属性** ```yaml props: sql-show: true # 开发环境建议开启 kernel-executor-size: 20 # 默认CPU核数*2 check-table-metadata-enabled: false # 生产环境建议关闭 ``` ### 最佳实践 1. **模块化配置** 按功能拆分为独立文件,启动时合并: ```bash bin/start.sh --config config/datasources.yaml \ --config config/sharding.yaml \ --config config/rw-splitting.yaml ``` 2. **配置校验** 使用内置工具检查语法: ```bash bin/validate-config.sh config/sharding.yaml ``` 3. **动态管理** 通过 DistSQL 运行时修改配置(需开启动态规则): ```sql -- 动态添加数据源 REGISTER STORAGE UNIT ds_new ( URL="jdbc:mysql://127.0.0.1:3306/db_new", USER="root", PASSWORD="root" ); -- 刷新元数据 REFRESH METADATA; ``` > **注意**:实际部署时需根据数据库类型调整驱动类名和连接参数,Spring Boot 项目建议使用 `shardingsphere-jdbc-core-spring-boot-starter` 依赖[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值