Codeforces 164B || Codeforces 163A

Codeforces 164B

是个字符串问题(codeforces把这类问题归为two pointers),给了2个字符串a,b都可以任意移位,且b中无相等元素,满足条件“是a的子串==是b的子序列”的最大的串

#include
#include
#include
#include
using namespace std;
int p[1000010],a[1000010],n,m,ans;
dequeq;
bool in(int x,int L,int R){
    if(L<=R)return L<=x&&x<=R;
    else return x<=R||L<=x;
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=0;i=min(m,n))
			break;
    }
    printf("%d\n",ans);
    return 0;
}


 Codeforces 163A

(x,y)其中x==y,x为字符串a的子串,y为字符串b的子序列,问这样的序偶有多少个?

解:这是个DP的问题

The problem could be solved with the following dynamic programming. Let f[i, j] be the number of distinct pairs (“substring starting at position i” and “subsequence of the substring t[j… |t|]”)

Then:

f[i, j] = f[i, j + 1];
if (s[i] == t[j])
  add(f[i, j], f[i + 1, j + 1] + 1)

Answer = f[i,0]

#include
#include
#include
#include
using namespace std;
#define mod 1000000007
int dp[5010][5010];
int main(){
	int i,j;
	char s1[5010],s2[5010];
	scanf("%s%s",s1,s2);
	int len1=strlen(s1),len2=strlen(s2);
	for(i=len1-1;i>=0;i--)
		for(j=len2-1;j>=0;j--){
			if(s1[i]==s2[j])
				dp[i][j]=(dp[i][j+1]+1+dp[i+1][j+1])%mod;
			else
				dp[i][j]=(dp[i][j+1])%mod;
		}
	int sum=0;
	for(i=len1-1;i>=0;i--)
		sum=(sum+dp[i][0])%mod;
	printf("%d\n",sum);
}


 

 

 


 

 

你可能感兴趣的:(codeforces)