【题目大意】

给出n个字符串,求有多少组字符串之间编辑距离为1~8。

n<=200,∑|S| <= 10^6

【题解】

首先找编辑距离有一个n^2的dp,由于发现只找小于等于8的,所以搜旁边16个状态即可。

复杂度O(n^2|S| * 16)

# include <vector>
# include <stdio.h>
# include <iostream>
# include <string.h>
# include <algorithm> using namespace std; typedef long long ll;
typedef unsigned long long ull;
typedef long double ld; const int N = + , M = 1e6 + , AN = ;
const int mod = 1e9 + ; int n, len[M], ans[AN];
vector<char> a[N];
char str[M]; int u, v;
int dp[][M];
inline int f(int i, int j) {
if(i == ) return j;
if(j == ) return i;
if(j < max(, i-) || j > min(len[v], i+)) return 1e9;
return dp[i&][j];
} inline void fin(int i, int j, int s) {
dp[i&][j] = s;
} inline int gans() {
for (int i=; i<=len[v] || i<=len[u]; ++i) fin(, i, ), fin(, i, );
for (int i=; i<=len[u]; ++i)
for (int j=max(, i-), jto = min(i+, len[v]); j<=jto; ++j) {
fin(i, j, min(f(i-, j), f(i, j-)) + );
if(a[u][i-] != a[v][j-]) fin(i, j, min(f(i, j), f(i-, j-) + ));
else fin(i, j, min(f(i, j), f(i-, j-)));
}
return f(len[u], len[v]);
} int main() {
// freopen("say.in", "r", stdin);
// freopen("say.out", "w", stdout);
cin >> n;
for (int i=; i<=n; ++i) {
scanf("%s", str);
len[i] = strlen(str);
for (int j=; str[j]; ++j) a[i].push_back(str[j]);
}
for (int i=, tem; i<=n; ++i) {
for (int j=i+; j<=n; ++j) {
u = i, v = j;
tem = gans();
if(tem >= && tem <= ) ++ ans[tem];
}
} for (int i=; i<=; ++i) cout << ans[i] << ' ';
cout << endl;
return ;
}

当然,过不了,有60分。

所以考虑dp有很多空余状态,改成dfs加剪枝就可以过了。。

# include <vector>
# include <stdio.h>
# include <iostream>
# include <string.h>
# include <algorithm> using namespace std; typedef long long ll;
typedef unsigned long long ull;
typedef long double ld; const int N = + , M = 1e6 + , AN = ;
const int mod = 1e9 + ; int n, len[M], ans[AN];
vector<char> a[N];
char str[M]; # define ABS(x) ((x) > ? (x) : -(x)) int tem;
inline void solve(int u, int v, int cur_u, int cur_v, int cnt) {
if(cnt + ABS(len[v] - len[u] - (cur_v - cur_u)) >= tem) return;
while(cur_u < len[u] && cur_v < len[v]) {
if(a[u][cur_u] != a[v][cur_v]) {
solve(u, v, cur_u+, cur_v, cnt+);
solve(u, v, cur_u, cur_v+, cnt+);
solve(u, v, cur_u+, cur_v+, cnt+);
return ;
}
++cur_u, ++cur_v;
}
if(cur_u == len[u]) tem = min(tem, cnt + len[v] - cur_v);
if(cur_v == len[v]) tem = min(tem, cnt + len[u] - cur_u);
} int main() {
// freopen("say.in", "r", stdin);
// freopen("say.out", "w", stdout);
cin >> n;
for (int i=; i<=n; ++i) {
scanf("%s", str);
len[i] = strlen(str);
for (int j=; str[j]; ++j) a[i].push_back(str[j]);
}
for (int i=; i<=n; ++i) {
for (int j=i+; j<=n; ++j) {
tem = ;
solve(i, j, , , );
++ ans[tem];
}
} for (int i=; i<=; ++i) cout << ans[i] << ' ';
cout << endl; return ;
}

这里dfs的时候需要注意一点,要一段一段跳,不能一格一格跳,会T。。

「6月雅礼集训 2017 Day1」说无可说的更多相关文章

  1. 「6月雅礼集训 2017 Day1」看无可看

    [题目大意] 给出n个数,a[1]...a[n],称作集合S,求

  2. 「6月雅礼集训 2017 Day10」quote

    [题目大意] 一个合法的引号序列是空串:如果引号序列合法,那么在两边加上同一个引号也合法:或是把两个合法的引号序列拼起来也是合法的. 求长度为$n$,字符集大小为$k$的合法引号序列的个数.多组数据. ...

  3. 「6月雅礼集训 2017 Day4」qyh(bzoj2687 交与并)

    原题传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2687 [题目大意] 给出若干区间,求一个区间的大于等于2的子集,使得 |区间并| 和 | ...

  4. 「6月雅礼集训 2017 Day11」delight

    [题目大意] 有$n$天,每天能吃饭.睡觉.什么事也不干 每天吃饭的愉悦值为$e_i$,睡觉的愉悦值为$s_i$,什么都不干愉悦值为0. 要求每连续$k$天都要有至少$E$天吃饭,$S$天睡觉. 求最 ...

  5. 「6月雅礼集训 2017 Day11」jump

    [题目大意] 有$n$个位置,每个位置有一个数$x_i$,代表从$i$经过1步可以到达的点在$[\max(1, i-x_i), \min(i+x_i, n)]$中. 定义$(i,j)$的距离表示从$i ...

  6. 「6月雅礼集训 2017 Day11」tree

    [题目大意] 给出一棵带权树,有两类点,一类黑点,一类白点. 求切断黑点和白点间路径的最小代价. $n \leq 10^5$ [题解] 直接最小割能过..但是树形dp明显更好写 设$f_{x,0/1/ ...

  7. 「6月雅礼集训 2017 Day10」perm(CodeForces 698F)

    [题目大意] 给出一个$n$个数的序列$\{a_n\}$,其中有些地方的数为0,要求你把这个序列填成一个1到$n$的排列,使得: $(a_i, a_j) = 1$,当且仅当$(i, j) = 1$.多 ...

  8. 「6月雅礼集训 2017 Day8」route

    [题目大意] 给出平面上$n$个点,求一条连接$n$个点的不相交的路径,使得转换的方向符合所给长度为$n-2$的字符串. $n \leq 5000$ [题解] 考虑取凸包上一点,然后如果下一个是‘R' ...

  9. 「6月雅礼集训 2017 Day8」gcd

    [题目大意] 定义times(a, b)表示用辗转相除计算a和b的最大公约数所需步骤. 那么有: 1. times(a, b) = times(b, a) 2. times(a, 0) = 0 3. ...

随机推荐

  1. springmvc+mybatis的两种配置和应用方式

    一.不用写dao层实现的方式 1.导入依赖包,我的pom.xml文件配置如下: <project xmlns="http://maven.apache.org/POM/4.0.0&qu ...

  2. Python3全栈学习目录

    http://www.cnblogs.com/wupeiqi/articles/4938499.html 文辉整理: http://blog.51cto.com/9272317/1869914

  3. Linux使用imagemagick的convert命令压缩图片,节省服务器空间

    1,安装imagemagick yum install ImageMagick 2,获取图片 find ./ -regex '.*\(jpg\|JPG\|png\|jpeg\)' -size +500 ...

  4. Jquery操作select选项集合!

    Query获取Select选择的Text和Value: 1. $("#select_id").change(function(){//code...}); //为Select添加事 ...

  5. 判断电脑CPU硬件支不支持64位

    你可以在注册表中查看: HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment\PROCESSO ...

  6. 「日常训练」Single-use Stones (CFR476D2D)

    题意(Codeforces 965D) $w$表示河的宽度,$l$表示青蛙所能跳的最远的距离,第二行的$w-1$个元素表示离河岸为$i$的地方有$a[i]$个石头,一个石头被踩两次,问最多有多少只青蛙 ...

  7. Go基础篇【第8篇】: 内置库模块 bytes [一]

    bytes包实现了操作[]byte的常用函数.本包的函数和strings包的函数相当类似. func Compare func Compare(a, b []byte) int Compare函数返回 ...

  8. URAL 1732 Ministry of Truth(KMP)

    Description In whiteblack on blackwhite is written the utterance that has been censored by the Minis ...

  9. Python两个内置函数——locals 和globals (学习笔记)

    这两个函数主要提供,基于字典的访问局部和全局变量的方式.在理解这两个函数时,首先来理解一下python中的名字空间概念.Python使用叫做名字空间的东西来记录变量的轨迹.名字空间只是一个字典,它的键 ...

  10. PAT 甲级 1042 Shuffling Machine

    https://pintia.cn/problem-sets/994805342720868352/problems/994805442671132672 Shuffling is a procedu ...