C++——USACO Section 2.3 题解

44 篇文章 0 订阅

Longest Prefix
IOI'96

The structure of some biological objects is represented by the sequence of their constituents, where each part is denoted by an uppercase letter. Biologists are interested in decomposing a long sequence into shorter sequences called primitives.

We say that a sequence S can be composed from a given set of primitives P if there is a some sequence of (possibly repeated) primitives from the set whose concatenation equals S. Not necessarily all primitives need be present. For instance the sequence ABABACABAABcan be composed from the set of primitives

	   {A, AB, BA, CA, BBC}

The first K characters of S are the prefix of S with length K. Write a program which accepts as input a set of primitives and a sequence of constituents and then computes the length of the longest prefix that can be composed from primitives.

PROGRAM NAME: prefix

INPUT FORMAT

First, the input file contains the list (length 1..200) of primitives (length 1..10) expressed as a series of space-separated strings of upper-case characters on one or more lines. The list of primitives is terminated by a line that contains nothing more than a period (`.'). No primitive appears twice in the list. Then, the input file contains a sequence S (length 1..200,000) expressed as one or more lines, none of which exceeds 76 letters in length. The "newlines" (line terminators) are not part of the string S.

SAMPLE INPUT (file prefix.in)

A AB BA CA BBC
.
ABABACABAABC

OUTPUT FORMAT

A single line containing an integer that is the length of the longest prefix that can be composed from the set P.

SAMPLE OUTPUT (file prefix.out)

11
/*
ID: mcdonne1
PROG: prefix
LANG: C++
*/
#include<iostream>
#include<cstdio>
using namespace std;
int cnt,ans;
string r,ch;
struct node{
	string s;
	int len;
}a[300];
bool step[200001];
int max(int a,int b)
{
	return a > b ? a : b ;
}
void f(int x)
{
	if(step[x]) return;
	ans=max(ans,x);
	step[x]=true;
	for(int i=0;i<cnt;++i)
		if(ch.compare(x,a[i].len,a[i].s)==0) f(x+a[i].len);
}
int main()
{
	freopen("prefix.in","r",stdin);
	freopen("prefix.out","w",stdout);
	while(cin>>a[cnt].s&&a[cnt].s!=".") a[cnt].len=a[cnt].s.length(),++cnt;
	while(cin>>r) ch+=r;
	for(int i=0;i<cnt;++i)
		if(ch.compare(0,a[i].len,a[i].s)==0) f(a[i].len);
	printf("%d\n",ans);
	return 0;
}

Cow Pedigrees

Silviu Ganceanu -- 2003

Farmer John is considering purchasing a new herd of cows. In this new herd, each mother cow gives birth to two children. The relationships among the cows can easily be represented by one or more binary trees with a total of N (3 <= N < 200) nodes. The trees have these properties:

  • The degree of each node is 0 or 2. The degree is the count of the node's immediate children.
  • The height of the tree is equal to K (1 < K < 100). The height is the number of nodes on the longest path from the root to any leaf; a leaf is a node with no children.

How many different possible pedigree structures are there? A pedigree is different if its tree structure differs from that of another pedigree. Output the remainder when the total number of different possible pedigrees is divided by 9901.

PROGRAM NAME: nocows

INPUT FORMAT

  • Line 1: Two space-separated integers, N and K.

SAMPLE INPUT (file nocows.in)

5 3

OUTPUT FORMAT

  • Line 1: One single integer number representing the number of possible pedigrees MODULO 9901.

SAMPLE OUTPUT (file nocows.out)

2

OUTPUT DETAILS

Two possible pedigrees have 5 nodes and height equal to 3:
           @                   @      
          / \                 / \
         @   @      and      @   @
        / \                     / \
       @   @                   @   @
/*
ID: mcdonne1
PROG: nocows
LANG: C++
*/
#include<cstdio>
int n,k;
int f[201][201];
int main()
{
	freopen("nocows.in","r",stdin);
	freopen("nocows.out","w",stdout);
	scanf("%d%d",&n,&k);
	for(int i=1;i<=k;++i) f[1][i]=1;
	for(int j=2;j<=k;++j)
		for(int i=1;i<=n;++i)
			for(int k=1;k<=i-2;++k)
			{
				f[i][j]+=f[k][j-1]*f[i-k-1][j-1];
				while (f[i][j]<=0) f[i][j]+=9901;
				f[i][j]%=9901;
			}
	printf("%d\n",(f[n][k]-f[n][k-1]+9901)%9901);
	return 0;
}
Zero Sum

Consider the sequence of digits from 1 through N (where N=9) in increasing order: 1 2 3 ... N.

Now insert either a `+' for addition or a `-' for subtraction or a ` ' [blank] to run the digits together between each pair of digits (not in front of the first digit). Calculate the result that of the expression and see if you get zero.

Write a program that will find all sequences of length N that produce a zero sum.

PROGRAM NAME: zerosum

INPUT FORMAT

A single line with the integer N (3 <= N <= 9).

SAMPLE INPUT (file zerosum.in)

7

OUTPUT FORMAT

In ASCII order, show each sequence that can create 0 sum with a `+', `-', or ` ' between each pair of numbers.

SAMPLE OUTPUT (file zerosum.out)

1+2-3+4-5-6+7
1+2-3-4+5+6-7
1-2 3+4+5+6+7
1-2 3-4 5+6 7
1-2+3+4-5+6-7
1-2-3-4-5+6+7
/*
ID: mcdonne1
PROG: zerosum
LANG: C++
*/
#include<cstdio>
#include<string>
int n;
std::string part[10][12]={
{""},{""},{""},{"1+2-3"},{"1-2-3+4"},{"1 2-3-4-5"},{"1 2+3-4-5-6"},{"1+2-3+4-5-6+7","1+2-3-4+5+6-7","1-2 3+4+5+6+7","1-2 3-4 5+6 7","1-2+3+4-5+6-7","1-2-3-4-5+6+7"},{"1 2-3 4-5 6+7 8","1+2 3-4 5+6+7+8","1+2+3+4-5-6-7+8","1+2+3-4+5-6+7-8","1+2-3+4+5+6-7-8","1+2-3-4-5-6+7+8","1-2 3-4+5+6+7+8","1-2+3-4-5+6-7+8","1-2-3+4+5-6-7+8","1-2-3+4-5+6+7-8"},
{"1 2+3 4-5 6-7+8+9","1 2+3+4-5-6-7+8-9","1 2+3-4 5+6+7+8+9","1 2+3-4+5-6+7-8-9","1 2-3+4+5 6-7 8+9","1 2-3+4+5+6-7-8-9","1 2-3-4-5+6-7-8+9","1 2-3-4-5-6+7+8-9","1+2-3 4-5 6+7 8+9","1-2 3-4-5 6-7+8 9","1-2-3 4+5+6+7+8+9"}
};
int main()
{
	freopen("zerosum.in","r",stdin);
	freopen("zerosum.out","w",stdout);
	scanf("%d",&n);
	for(int i=0;part[n][i]!="";++i)
		printf("%s\n",part[n][i].c_str());
	return 0;
}

Money Systems

The cows have not only created their own government but they have chosen to create their own money system. In their own rebellious way, they are curious about values of coinage. Traditionally, coins come in values like 1, 5, 10, 20 or 25, 50, and 100 units, sometimes with a 2 unit coin thrown in for good measure.

The cows want to know how many different ways it is possible to dispense a certain amount of money using various coin systems. For instance, using a system of {1, 2, 5, 10, ...} it is possible to create 18 units several different ways, including: 18x1, 9x2, 8x2+2x1, 3x5+2+1, and many others.

Write a program to compute how many ways to construct a given amount of money using supplied coinage. It is guaranteed that the total will fit into both a signed long long (C/C++) and Int64 (Free Pascal).

PROGRAM NAME: money

INPUT FORMAT

The number of coins in the system is V (1 <= V <= 25).

The amount money to construct is N (1 <= N <= 10,000).

Line 1:Two integers, V and N
Lines 2..:V integers that represent the available coins (no particular number of integers per line)

SAMPLE INPUT (file money.in)

3 10
1 2 5

OUTPUT FORMAT

A single line containing the total number of ways to construct N money units using V coins.

SAMPLE OUTPUT (file money.out)

10
/*
ID: mcdonne1
PROG: money
LANG: C++
*/
#include<cstdio>
int v,n;
int a[30];
long long dp[10001];
int main()
{
	freopen("money.in","r",stdin);
	freopen("money.out","w",stdout);
	scanf("%d%d",&v,&n);
	for(int i=0;i<v;++i) scanf("%d",&a[i]);
	for(int i=0;i<=n;++i)
		if(i%a[0]==0)
			dp[i]=1;
	for(int i=1;i<v;++i)
		for(int j=1;j<=n;++j)
			if(j>=a[i])
				dp[j]+=dp[j-a[i]];
	printf("%lld\n",dp[n]);
	return 0;
}
Controlling Companies

Some companies are partial owners of other companies because they have acquired part of their total shares of stock. For example, Ford at one point owned 12% of Mazda. It is said that a company A controls company B if at least one of the following conditions is satisfied:

  • Company A = Company B
  • Company A owns more than 50% of Company B
  • Company A controls K (K >= 1) companies denoted C1, ..., CK with each company Ci owning xi% of company B and x1 + .... + xK > 50%.

Given a list of triples (i,j,p) which denote company i owning p% of company j, calculate all the pairs (h,s) in which company h controls company s. There are at most 100 companies.

Write a program to read the list of triples (i,j,p) where i, j and p are positive integers all in the range (1..100) and find all the pairs (h,s) so that company h controls company s.

PROGRAM NAME: concom

INPUT FORMAT

Line 1:n, the number of input triples to follow
Line 2..n+1:Three integers per line as a triple (i,j,p) described above.

SAMPLE INPUT (file concom.in)

3
1 2 80
2 3 80
3 1 20

OUTPUT FORMAT

List 0 or more companies that control other companies. Each line contains two integers that denote that the company whose number is the first integer controls the company whose number is the second integer. Order the lines in ascending order of the first integer (and ascending order of the second integer to break ties). Do not print that a company controls itself.

SAMPLE OUTPUT (file concom.out)

1 2
1 3
2 3
/*
ID: mcdonne1
PROG: concom
LANG: C++
*/
#include<cstdio>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
int n,x,y,z,cnt;
int f[111][111];
map <int,int> m;
map <int,int> t;
vector < pair<int,int> > v;
bool cmp(pair <int,int> a,pair <int,int> b)
{
	return a.first!=b.first ? a.first<b.first : a.second < b.second;
}
int main()
{
	freopen("concom.in","r",stdin);
	freopen("concom.out","w",stdout);
	scanf("%d",&n);
	for(int i=1;i<=n;++i)
	{
		scanf("%d%d%d",&x,&y,&z);
		if(!m[x]) m[x]=++cnt,t[cnt]=x;
		if(!m[y]) m[y]=++cnt,t[cnt]=y;
		f[m[x]][m[y]]=z;
	}
	for(int k=1;k<=cnt;++k)
		for(int i=1;i<=cnt;++i)
			for(int j=1;j<=cnt;++j)
				if(i!=j&&j!=k&&k!=i&&f[i][k]>=50)
					f[i][j]+=f[k][j];
	for(int i=1;i<=cnt;++i)
		for(int j=1;j<=cnt;++j)
			if(f[i][j]>=50)
				v.push_back(make_pair(t[i],t[j]));
	sort(v.begin(),v.end(),cmp);
	for(int i=0,j=v.size();i<j;++i)
		if(v[i].first!=v[i].second)
			printf("%d %d\n",v[i].first,v[i].second);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值