4 substrings problem
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 583 Accepted Submission(s): 180
Problem Description
One day you heard the following joke
In America, you write strings.
In Soviet Russia, String writes YOU!! 1
And find that now string is writting you! So to get rid of it, you must solve the following problem:
Given a string S and its four substring a,b,c, and d. In a configuration, you can place four substrings exactly one position it occurs in S (they may overlap), and characters covered by at least one such substring is called “covered”.
You should solve for minimum and maximum possible number of covered characters in a configuration.
You may assume that s contains only lowercase letters, and is of length less than 4096. However, lengths of a,b,c, and d would never exceed 64.
-------------------------------------------------------------------
1An infamous Russian reversal
In America, you write strings.
In Soviet Russia, String writes YOU!! 1
And find that now string is writting you! So to get rid of it, you must solve the following problem:
Given a string S and its four substring a,b,c, and d. In a configuration, you can place four substrings exactly one position it occurs in S (they may overlap), and characters covered by at least one such substring is called “covered”.
You should solve for minimum and maximum possible number of covered characters in a configuration.
You may assume that s contains only lowercase letters, and is of length less than 4096. However, lengths of a,b,c, and d would never exceed 64.
-------------------------------------------------------------------
1An infamous Russian reversal
Input
There are several test cases.
For each test case there are 5 lines, denoting S,a,b,c, and d, respectively.
Please process until the EOF (End Of File).
For each test case there are 5 lines, denoting S,a,b,c, and d, respectively.
Please process until the EOF (End Of File).
Output
For each test case, please print a single line with two integers, first the minimum, then the maximum.
Sample Input
hello he l l o abacaba ab ba a c
Sample Output
4 5 4 6
Source
Recommend
liuyiding
状压大法好
先暴力处理每个字串在原串中的位置。。
用dp[i][j][k]表示主串选到第i位了,a,b,c,d四个字串有没有选过(2^4),从这一位开始后面已经放定的长度为k,表示的最大值或最小值
然后用队列维护一下即可。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define max(a,b) (a>=b ? a:b)
#define min(a,b) (a<=b ? a:b)
#define inf 0x3f3f3f3f
using namespace std;
char s[4100],ss[4][66];
bool check[4][4100];
int len[4];
void read(){
for(int i=0;i<4;i++) scanf("%s",ss[i]+1);
}
int dp[4100][16][66];
struct node{
int x,st,l;
node(int x,int st,int l):x(x),st(st),l(l){}
};
int bfs(){
queue<node> q;
q.push(node(0,0,0));
memset(dp,-1,sizeof dp);
dp[0][0][0]=0;
int l=strlen(s+1);
while(!q.empty()){
node v=q.front();
q.pop();
if(v.x==l+1) continue;
for(int i=0;i<4;i++){
if(v.st&(1<<i)) continue;
int ll;
if(check[i][v.x]){
ll=max(v.l,len[i]);
if(dp[v.x][v.st|(1<<i)][ll]==-1){
q.push(node(v.x,v.st|(1<<i),ll));
}
dp[v.x][v.st|(1<<i)][ll]=max(dp[v.x][v.st|(1<<i)][ll],dp[v.x][v.st][v.l]);
}
}
int ll=max(v.l-1,0);
if(dp[v.x+1][v.st][ll]==-1){
q.push(node(v.x+1,v.st,ll));
}
dp[v.x+1][v.st][ll]=max(dp[v.x+1][v.st][ll],dp[v.x][v.st][v.l]+(v.l ? 1:0));
}
int ret=-1;
for(int i=1;i<=l;i++){
ret=max(ret,dp[i][15][1]+1);
}
if(ret==-1) return 0;
else return ret;
}
int bfs1(){
queue<node> q;
q.push(node(0,0,0));
memset(dp,0x3f,sizeof dp);
dp[0][0][0]=0;
int l=strlen(s+1);
while(!q.empty()){
node v=q.front();
q.pop();
//cout<<v.x<<" "<<v.st<<" "<<v.l<<" "<<dp[v.x][v.st][v.l]<<endl;
if(v.x==l+1) continue;
for(int i=0;i<4;i++){
if(v.st&(1<<i)) continue;
int ll;
if(check[i][v.x]){
ll=max(v.l,len[i]);
if(dp[v.x][v.st|(1<<i)][ll]==inf){
q.push(node(v.x,v.st|(1<<i),ll));
}
dp[v.x][v.st|(1<<i)][ll]=min(dp[v.x][v.st|(1<<i)][ll],dp[v.x][v.st][v.l]);
}
}
int ll=max(v.l-1,0);
if(dp[v.x+1][v.st][ll]==inf){
q.push(node(v.x+1,v.st,ll));
}
dp[v.x+1][v.st][ll]=min(dp[v.x+1][v.st][ll],dp[v.x][v.st][v.l]+(v.l ? 1:0));
}
int ret=inf;
for(int i=1;i<=l;i++){
ret=min(ret,dp[i][15][1]+1);
}
if(ret==inf) return 0;
else return ret;
}
void solve(){
memset(check,0,sizeof check);
int l=strlen(s+1);
for(int i=0;i<4;i++){
len[i]=strlen(ss[i]+1);
for(int j=1;j<=l;j++){
if(s[j]==ss[i][1]){
int k=1,tmp=j,jj=j;
while(k<=len[i]&&s[jj]==ss[i][k]) jj++,k++;
if(k==len[i]+1) {
check[i][tmp]=1;
}
}
}
}
printf("%d %d\n",bfs1(),bfs());
}
int main(){
//freopen("input","r",stdin);
//freopen("out2","w",stdout);
while(~scanf("%s",s+1)){
read();
solve();
}
return 0;
}