锘??xml version="1.0" encoding="utf-8" standalone="yes"?> Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make changes with these coins for a given amount of money. 棰樼洰澶ф剰錛?/p> 緇欏畾1 5 10 25 50浜旂紜竵錛岀粰瀹氫竴涓暟錛岄棶鐢ㄨ繖浜涚‖甯佹湁澶氬皯縐嶄笉鍚岀殑緇勫悎鏂瑰紡錛?/p> 鎯蟲兂濂藉儚鍙互鏄竴涓柟紼嬬粍姹傝В鐨勯棶棰樺槢錛?/p> x1+5*x2+10*x3+25*x4+50*x5=x0; 緇欏畾x0錛屼竴緇勬弧瓚蟲潯浠剁殑x>=0灝辨槸瑙i泦銆傚晩鍝堝搱鍝堛傚彲浠ラ珮鏂秷鍏冩硶鍜?/p> 涓嶈繃錛屾槑鏄劇殑璁℃暟闂錛?/p> dp[i][j]琛ㄧずi鍙互鐢卞墠j縐嶇‖甯佽〃紺虹殑鏂規硶鏁般?/p> dp[i][j]=dp[i][j-1]+dp[i-coin[j]][j] i-coin[j]>0 dp[i][j]=dp[i][j-1]+1 i-coin[j]=0; dp[i][j]=dp[i][j-1] i-coin[j]<0 鏂圭▼綾婚棶棰橈紝涓瀹氳鍏堟妸鏂圭▼鍐欐竻妤氾紒錛侊紒 Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make changes with these coins for a given amount of money. 棰樼洰澶ф剰錛?/p> 緇欏畾1 5 10 25 50浜旂紜竵錛岀粰瀹氫竴涓暟錛岄棶鐢ㄨ繖浜涚‖甯佹湁澶氬皯縐嶄笉鍚岀殑緇勫悎鏂瑰紡錛?/p> 鎯蟲兂濂藉儚鍙互鏄竴涓柟紼嬬粍姹傝В鐨勯棶棰樺槢錛?/p> x1+5*x2+10*x3+25*x4+50*x5=x0; 緇欏畾x0錛屼竴緇勬弧瓚蟲潯浠剁殑x>=0灝辨槸瑙i泦銆傚晩鍝堝搱鍝堛傚彲浠ラ珮鏂秷鍏冩硶鍜?/p> 涓嶈繃錛屾槑鏄劇殑璁℃暟闂錛?/p> dp[i][j]琛ㄧずi鍙互鐢卞墠j縐嶇‖甯佽〃紺虹殑鏂規硶鏁般?/p> dp[i][j]=dp[i][j-1]+dp[i-coin[j]][j] i-coin[j]>0 dp[i][j]=dp[i][j-1]+1 i-coin[j]=0; dp[i][j]=dp[i][j-1] i-coin[j]<0 鏂圭▼綾婚棶棰橈紝涓瀹氳鍏堟妸鏂圭▼鍐欐竻妤氾紒錛侊紒
Summer Training #2 DIV1錛岃瀹岃檺錛?br />
棰樻剰璇諱簡涓や釜澶氬皬鏃訛紒錛侊紒錛?br />
棰樻剰涓嶈浜嗭紝
絎竴縐嶅仛娉曪細
#define maxm 120
using namespace std;
char a[maxn],b[maxm];
int cnt[27],p[27][maxn],now[maxm];
int main()
{
scanf("%s",a);
scanf("%s",b);
int n=strlen(a);
int m=strlen(b);
clr(cnt);
for (int i=0;i<n;i++)
{
int tmp=a[i]-'a';
p[tmp][cnt[tmp]++]=i;
}
long long ans=0;
clr(now);
for (int i=0;i<n;i++)
{
int last=i-1;
int begin;
bool flag=1;
for (int j=0;j<m;j++)
{
int tmp=b[j]-'a';
while (now[j]<cnt[tmp] && p[tmp][now[j]]<=last)
now[j]++;
if (now[j]==cnt[tmp])
{
flag=0;
break;
}
last=p[tmp][now[j]];
if (j==0)
begin=last;
}
if (!flag)
break;
ans+=(n-last)*(begin-i+1);
i=begin;
}
printf("%I64d\n",ans);
return 0;
}
寰堣寮傜殑鏄悗闈㈣繖涓狣P鍋氭硶錛岀涓嬈℃彁浜ょ殑鏃跺欏眳鐒訛細
#define maxm 120
using namespace std;
char a[maxn],b[maxm];
int dp[maxn][maxm];
int main()
{
scanf("%s%s",a,b);
int n=strlen(a);
int m=strlen(b);
clr(dp);
for (int i=0;i<=n;i++)
dp[i][0]=1;
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
{
if (a[i-1]==b[j-1])
dp[i][j]+=dp[i-1][j-1];
else
dp[i][j-1]+=dp[i-1][j-1];
}
}
long long ans=0;
for (int i=m;i<=n;i++)
ans+=dp[i][m]*(n-i+1);
printf("%I64d\n",ans);
return 0;
}
]]>
]]>
http://acm.hust.edu.cn:8080/judge/problem/viewProblem.action?id=19242
棰樼洰澶ф剰錛?br /> 鍒囨按鏋滄父鎴忥紝鍦ㄥ睆騫曚笂浠繪剰鏃跺埢錛屼綘鍙互鍒囨帀灞忓箷涓婃墍鏈夌殑姘存灉銆傚鏋滃垏鎺夌殑姘存灉鏁板ぇ浜?錛岄偅鎵寰楀垎灝辨槸姘存灉鏁幫紝鍚﹀垯涓?.
緇檔涓按鏋滃簭鍒梉st,et]鍑虹幇鏃墮棿鍜岀粨鏉熸椂闂達紝闂渶鍚庢渶澶氳兘鑾峰緱澶氬皯鍒嗐?br />
DP+紱繪暎鍖栵紙涓嶆噦浠涔堝彨紱繪暎鍖栵紒錛?br /> dp[i]=max{dp[j-1]+cute[j,i],1<=j<=i}
浠ュ紑濮嬫椂闂撮掑鎺掑簭姹俢ute[j,i]
#include<string.h>
#include<math.h>
int x[1005],y[1005],dp[1005];
int n,ans;
void qxsort(int l,int r)
{
int i,j,mid,tmp;
i=l;
j=r;
mid=x[(i+j)/2];
while (i<=j)
{
while (x[i]<mid) i++;
while (x[j]>mid) j--;
if (i<=j)
{
tmp=x[i];
x[i]=x[j];
x[j]=tmp;
tmp=y[i];
y[i]=y[j];
y[j]=tmp;
i++;
j--;
}
}
if (l<j) qxsort(l,j);
if (i<r) qxsort(i,r);
}
int max(int a,int b)
{
if (a>b)
return a;
return b;
}
void work()
{
int i,j,s,now;
memset(dp,0,sizeof(dp));
qxsort(1,n);
ans=0;
for (i=3;i<=n ;i++ )
{
if (i+1<=n && x[i]==x[i+1]) continue;
s=0;
for (j=i;j>=1 ;j-- )
{
if (x[j]<=x[i] && y[j]>=x[i]) s++;
now=s;
if (now<3)
now=0;
dp[i]=max(dp[i],dp[j-1]+now);
}
ans=max(ans,dp[i]);
}
}
int main()
{
int t,i,cas=0;
scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
for (i=1;i<=n ;i++ )
scanf("%d%d",&x[i],&y[i]);
work();
printf("Case #%d: %d\n",++cas,ans);
}
return 0;
}
鍝庯紝鐪熸槸寮辯垎浜嗭紝鐪嬫潵dp棰樼洰鐪熺殑寰堝寰堝彉鎬佸晩銆傝澶氭兂鎯充簡
榪樻槸錛岄棶棰樻湰璐ㄥ緢閲嶈錛侊紒錛侊紒
]]>Coin Change
For example, if we have 11 cents, then we can make changes with one 10-cent coin and one 1-cent coin, two 5-cent coins and one 1-cent coin, one 5-cent coin and six 1-cent coins, or eleven 1-cent coins. So there are four ways of making changes for 11 cents with the above coins. Note that we count that there is one way of making change for zero cent.
Write a program to find the total number of different ways of making changes for any amount of money in cents. Your program should be able to handle up to 7489 cents. Input
The input file contains any number of lines, each one consisting of a number for the amount of money in cents. Output
For each input line, output a line containing the number of different ways of making changes with the above 5 types of coins. Sample Input
11 26
Sample Output
4 13
#include<string.h>
#include<math.h>
long long dp[7500][5],coin[5]={1,5,10,25,50};
int GetAns()
{
int i,j;
memset(dp,0,sizeof(dp));
dp[1][0]=dp[1][1]=dp[1][2]=dp[1][3]=dp[1][4]=1;
for (i=2;i<=7489 ;i++ )
{
dp[i][0]=1;
for (j=1;j<5 ;j++ )
if (i-coin[j]>0)
dp[i][j]=dp[i][j-1]+dp[i-coin[j]][j];
else
if (i-coin[j]==0)
dp[i][j]=dp[i][j-1]+1;
else
dp[i][j]=dp[i][j-1];
}
}
int main()
{
int n;
GetAns();
while (scanf("%d",&n)==1)
printf("%lld\n",dp[n][4]);
return 0;
}
棰濋錛宒p鏄釜濂戒笢瑗匡紝璇ョ湅鐪嬩紭鍖栦簡錛侊紒錛?img src ="http://www.shnenglu.com/ArcTan/aggbug/173165.html" width = "1" height = "1" />
]]>Coin Change
For example, if we have 11 cents, then we can make changes with one 10-cent coin and one 1-cent coin, two 5-cent coins and one 1-cent coin, one 5-cent coin and six 1-cent coins, or eleven 1-cent coins. So there are four ways of making changes for 11 cents with the above coins. Note that we count that there is one way of making change for zero cent.
Write a program to find the total number of different ways of making changes for any amount of money in cents. Your program should be able to handle up to 7489 cents. Input
The input file contains any number of lines, each one consisting of a number for the amount of money in cents. Output
For each input line, output a line containing the number of different ways of making changes with the above 5 types of coins. Sample Input
11 26
Sample Output
4 13
#include<string.h>
#include<math.h>
long long dp[7500][5],coin[5]={1,5,10,25,50};
int GetAns()
{
int i,j;
memset(dp,0,sizeof(dp));
dp[1][0]=dp[1][1]=dp[1][2]=dp[1][3]=dp[1][4]=1;
for (i=2;i<=7489 ;i++ )
{
dp[i][0]=1;
for (j=1;j<5 ;j++ )
if (i-coin[j]>0)
dp[i][j]=dp[i][j-1]+dp[i-coin[j]][j];
else
if (i-coin[j]==0)
dp[i][j]=dp[i][j-1]+1;
else
dp[i][j]=dp[i][j-1];
}
}
int main()
{
int n;
GetAns();
while (scanf("%d",&n)==1)
printf("%lld\n",dp[n][4]);
return 0;
}
棰濋錛宒p鏄釜濂戒笢瑗匡紝璇ョ湅鐪嬩紭鍖栦簡錛侊紒錛?img src ="http://www.shnenglu.com/ArcTan/aggbug/173166.html" width = "1" height = "1" />
]]>