专题二1024

*题目编号:1024

*简单题意:求给定数的子串

*解题思路:DFS,从第一个数开始,找比它大的数,同时记录子串大小,知道找不到比他大的,继续第二个数查找

*感想:24题和25差不多

*AC源码

 # include <iostream>  
 # include <cstdio>  
 # include <cstdlib>  
 # include <algorithm>  
using namespace std;  
  
int n, p, len, Count;  
int num[1001];  
bool flag;  
struct Tem  
{  
    int n, pos;  
};  
  
Tem tem[1001];  
  
bool check(int from, int cur)  
{  
    for (int i = from + 1; i < cur; i++)  
        if (num[i] == num[cur])  
            return false;  
    return true;  
}  
void print(int length)  
{  
    for (int i = 0; i < length - 1; i++)  
        cout << tem[i].n << " ";  
    cout << tem[length - 1].n << endl;  
}  
  
void dfs(int dep, int pos)  
{  
    if (Count >= p)return;  
    if (dep == len)  
    {  
        Count++;  
        flag = true;  
        print(len);  
        return;  
    }  
    for (int i = pos; i<n; i++)  
    {  
        if ((dep != 0 && tem[dep - 1].n <= num[i]) || dep == 0)  
        {  
            if (dep == 0 && !check(-1, i))  
                continue;  
            if (dep != 0 && !check(tem[dep - 1].pos, i))  
                continue;  
            tem[dep].n = num[i];  
            tem[dep].pos = i;  
            dfs(dep + 1, i + 1);  
        }  
    }  
    return;  
}  
  
int main()  
{  
    while (cin >> n >> p)  
    {  
        for (int i = 0; i<n; i++)  
            cin >> num[i];  
        Count = 0;  
        for (int i = 1; i < n; i++)  
        {  
            flag = false;  
            len = i;  
            dfs(0, 0);  
            if (Count >=p || !flag)break;  
        }  
        cout << endl;  
    }  
    return 0;  
}  




Sequence one

Time Limit : 6000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 17   Accepted Submission(s) : 9
Problem Description
Search is important in the acm algorithm. When you want to solve a problem by using the search method, try to cut is very important.<br>Now give you a number sequence, include n (&lt;=1000) integers, each integer not bigger than 2^31, you want to find the first P subsequences that is not decrease (if total subsequence W is smaller than P, than just give the first W subsequences). The order of subsequences is that: first order the length of the subsequence. Second order the sequence of each integer’s position in the initial sequence. For example initial sequence 1 3 2 the total legal subsequences is 5. According to order is {1}; {3}; {2}; {1,3}; {1,2}. {1,3} is first than {1,2} because the sequence of each integer’s position in the initial sequence are {1,2} and {1,3}. {1,2} is smaller than {1,3}. If you also can not understand , please see the sample carefully. <br>
 

Input
The input contains multiple test cases.<br>Each test case include, first two integers n, P. (1<n<=1000, 1<p<=10000). <br>
 

Output
For each test case output the sequences according to the problem description. And at the end of each case follow a empty line.
 

Sample Input
   
   
3 5<br>1 3 2<br>3 6<br>1 3 2<br>4 100<br>1 2 3 2<br>
 

Sample Output
   
   
1<br>3<br>2<br>1 3<br>1 2<br><br>1<br>3<br>2<br>1 3<br>1 2<br><br>1<br>2<br>3<br>1 2<br>1 3<br>2 3<br>2 2<br>1 2 3<br>1 2 2<br><br><br><div style='font-family:Times New Roman;font-size:14px;background-color:F4FBFF;border:#B7CBFF 1px dashed;padding:6px'><div style='font-family:Arial;font-weight:bold;color:#7CA9ED;border-bottom:#B7CBFF 1px dashed'><i>Hint</i></div>Hint : You must make sure each subsequence in the subsequences is unique.</div>
 

Author
yifenfei
 

Source
奋斗的年代
 

Statistic |  Submit |  Back

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值