首页 技术 正文
技术 2022年11月15日
0 收藏 308 点赞 2,479 浏览 3129 个字

题解:

构建出trie图,令f[i][j]表示到第i个字符走到j号节点最少需要修改的字符数,然后枚举后继节点转移即可。

代码:没写caseWA了n发。。。

 #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 2000+5
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define for4(i,x) for(int i=head[x],y;i;i=e[i].next)
#define mod 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
int n,tot,f[maxn][maxn],t[maxn][],go[maxn];
char s[maxn];
bool v[maxn];
queue<int>q;
inline int id(char ch)
{
if(ch=='A')return ;
if(ch=='G')return ;
if(ch=='C')return ;
if(ch=='T')return ;
}
inline void insert()
{
memset(s,,sizeof(s));
scanf("%s",s+);int len=strlen(s+),now=;
for1(i,len)
{
int x=id(s[i]);
if(!t[now][x])t[now][x]=++tot;
now=t[now][x];
}
v[now]=;
}
void bfs()
{
q.push();
while(!q.empty())
{
int x=q.front(),y,j;v[x]|=v[go[x]];q.pop();
for0(i,)
{
j=go[x];
while(j&&!t[j][i])j=go[j];
if(t[x][i])
{
go[y=t[x][i]]=j?t[j][i]:;
q.push(y);
}else t[x][i]=j?t[j][i]:;
}
}
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int cs=;
while(cin>>n&&n)
{
tot=;
memset(t,,sizeof(t));
memset(v,,sizeof(v));
memset(go,,sizeof(go));
for1(i,n)insert();
bfs();
memset(s,,sizeof(s));
scanf("%s",s+);n=strlen(s+);
for0(i,n)for1(j,tot)f[i][j]=inf;
f[][]=;
for0(i,n-)
for1(j,tot)
if(f[i][j]!=inf)
for0(k,)
if(!v[t[j][k]])
f[i+][t[j][k]]=min(f[i+][t[j][k]],f[i][j]+(k!=id(s[i+])));
int ans=inf;
for1(i,tot)ans=min(ans,f[n][i]);
printf("Case %d: %d\n",++cs,ans==inf?-:ans);
}
return ;
}

DNA repair

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5712   Accepted: 2677

Description

Biologists finally invent techniques of repairing DNA that contains segments causing kinds of inherited diseases. For the sake of simplicity, a DNA is represented as a string containing characters ‘A’, ‘G’ , ‘C’ and ‘T’. The repairing techniques are simply to change some characters to eliminate all segments causing diseases. For example, we can repair a DNA “AAGCAG” to “AGGCAC” to eliminate the initial causing disease segments “AAG”, “AGC” and “CAG” by changing two characters. Note that the repaired DNA can still contain only characters ‘A’, ‘G’, ‘C’ and ‘T’.

You are to help the biologists to repair a DNA by changing least number of characters.

Input

The input consists of multiple test cases. Each test case starts with a line containing one integers N (1 ≤ N ≤ 50), which is the number of DNA segments causing inherited diseases.
The following N lines gives N non-empty strings of length not greater than 20 containing only characters in “AGCT”, which are the DNA segments causing inherited disease.
The last line of the test case is a non-empty string of length not greater than 1000 containing only characters in “AGCT”, which is the DNA to be repaired.

The last test case is followed by a line containing one zeros.

Output

For each test case, print a line containing the test case number( beginning with 1) followed by the
number of characters which need to be changed. If it’s impossible to repair the given DNA, print -1.

Sample Input

2
AAA
AAG
AAAG
2
A
TG
TGAATG
4
A
G
C
T
AGT
0

Sample Output

Case 1: 1
Case 2: 4
Case 3: -1
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905