C++——USACO Section 2.3 题解

来源:互联网 发布:golang for sleep 编辑:程序博客网 时间:2024/04/30 11:09

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: mcdonne1PROG: prefixLANG: 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: mcdonne1PROG: nocowsLANG: 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+71+2-3-4+5+6-71-2 3+4+5+6+71-2 3-4 5+6 71-2+3+4-5+6-71-2-3-4-5+6+7
/*ID: mcdonne1PROG: zerosumLANG: 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 NLines 2..:V integers that represent the available coins (no particular number of integers per line)

SAMPLE INPUT (file money.in)

3 101 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: mcdonne1PROG: moneyLANG: 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 followLine 2..n+1:Three integers per line as a triple (i,j,p) described above.

SAMPLE INPUT (file concom.in)

31 2 802 3 803 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 21 32 3
/*ID: mcdonne1PROG: concomLANG: 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;}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 u盘被要求格式化怎么办 u盘弹出要格式化怎么办 u盘无法格式化怎么办啊 大盘下跌下你该怎么办 股票想卖没人买怎么办 百变模板乱了怎么办 5个半月宝宝胀气怎么办 三岁宝宝胃胀怎么办 全民k歌不能用wf怎么办 雅乐之舞掉叶子怎么办 姬珊瑚发软歪了怎么办 胃胀气怎么办简单的方法 8个月的宝宝咳嗽怎么办 坐完月子掉头发怎么办 在香港买到假货怎么办 用气垫脸上浮粉怎么办 不够奶给宝宝吃怎么办 叶插发芽发根后怎么办 多肉植物摊大饼怎么办 英短蓝猫掉毛怎么办 英短蓝猫很凶怎么办 面部打伤怎么办了肿了 朋友欠钱一直拖怎么办 旧车三年不年检怎么办 遇到领导整你该怎么办 皮肤热了就瘙痒怎么办 猫爪子肉垫脏了怎么办 嘴周围干燥起皮怎么办 下嘴唇总是起皮怎么办 上嘴唇老是起皮怎么办 涂口红嘴唇起皮怎么办 上嘴唇干裂起皮怎么办 9岁儿童嘴唇干裂怎么办 上嘴唇干裂烂了怎么办 孕妇脚干的裂开怎么办 小腿皮肤干燥起皮怎么办 秋天脸干燥起皮怎么办 脚上干燥起皮怎么办 脚皮肤干燥起皮怎么办 脚有脚气脱皮又痒怎么办 脚特别干还脱皮怎么办