2013
12-10

Fourier’s Lines

Joseph Fourier was a great mathematician and physicist and is well known for his mathematic series. Among all the nineteen children in his family, Joseph was the youngest and the smartest. He began to show his interest in mathematics when he was very young. After he grew up, he often corresponded with C. Bonard (a professor of mathematics at Auxerre) by exchanging letters.

In one letter written to Bonard, Fourier asked a question: how to draw 17 lines on a plane to make exactly 101 crossings, where each crossing belongs to exactly two lines. Obviously, this is an easy problem, and Figure-1 is a solution that satisfies his requirement. Now the problem for you is a universal one. Can we draw N lines on a plane to make exactly M crossings, where each crossing belongs to exactly two lines? If we can, how many pieces, at most, can these lines cut the plane into?

The input may have several sets of test data. Each set is one line containing two integers N and M (1 <= N <= 100, 0 <= M <= 10000), separated by a space. The test data is followed by a line containing two zeros, which indicates the end of input and should not be processed as a set of data.

Output one line for each set of input in the following format:

Case i: N lines cannot make exactly M crossings.

if the drawing of these lines is impossible;

or:

Case i: N lines with exactly M crossings can cut the plane into K pieces at most.

Note: Even if N or M equals to one, you should use the words "lines" and "crossings" in your output.

4 3
4 6
4 2
5 11
17 101
0 0

Case 1: 4 lines with exactly 3 crossings can cut the plane into 8 pieces at most.
Case 2: 4 lines with exactly 6 crossings can cut the plane into 11 pieces at most.
Case 3: 4 lines cannot make exactly 2 crossings.
Case 4: 5 lines cannot make exactly 11 crossings.
Case 5: 17 lines with exactly 101 crossings can cut the plane into 119 pieces at most.

#include <iostream>
#include <string>
using namespace std;
string a[105];
string b[105];
string c[105];
int dp[105][105];
int d[105][105]; //记录路径
int len1,len2;
int main(){
while(cin>>a[1]){
len1 = 2;
while(a[len1 - 1] != "#")
cin>>a[len1++];
len1 -= 2;
cin>>b[1];
len2 = 2;
while(b[len2 - 1] != "#")
cin>>b[len2++];
len2 -= 2;
memset(dp,0,sizeof(dp));
memset(d,0,sizeof(d));
for(int i = 1; i <= len1; i++)
for(int j = 1; j <= len2; j++){
if(a[i] == b[j])
dp[i][j] = dp[i - 1][j - 1] + 1,d[i][j] = 1;
else{
if(dp[i -1][j] > dp[i][j - 1])
dp[i][j] = dp[i - 1][j],d[i][j] = 2;
else
dp[i][j] = dp[i][j - 1],d[i][j] = 3;
}
}
int n = dp[len1][len2];
int i = len1;
int j = len2;
while(n){
while(d[i][j] != 1){
if(d[i][j] == 2)
i--;
else
j--;
}
n--;
c[n] = a[i];
i--;
j--;
}
cout<<c[0];
for(int i =1; i < dp[len1][len2]; i++)
cout<<" "<<c[i];
cout<<endl;
}
return 0;
}

1. for(int i=1; i<=m; i++){
for(int j=1; j<=n; j++){
dp = dp [j-1] + 1;
if(s1.charAt(i-1) == s3.charAt(i+j-1))
dp = dp[i-1] + 1;
if(s2.charAt(j-1) == s3.charAt(i+j-1))
dp = Math.max(dp [j - 1] + 1, dp );
}
}
这里的代码似乎有点问题？ dp(i)(j) = dp(i)(j-1) + 1;这个例子System.out.println(ils.isInterleave("aa","dbbca", "aadbbcb"));返回的应该是false

2. 有两个重复的话结果是正确的，但解法不够严谨，后面重复的覆盖掉前面的，由于题目数据限制也比较严，所以能提交通过。已更新算法