单调递增最长子序列 怎么解决时间超限

clearscout 2017-10-19 01:51:00
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string>
#include<algorithm>
using namespace std;
struct Mystruct
{
char ch;
int maxLengh;
};
int cmp(int n,Mystruct *m,char ch)
{
for (int i = 0; i <= n; i++)
{
if (m[i].ch < ch)
{
if (m[n+1].maxLengh<(m[i].maxLengh + 1))
m[n+1].maxLengh = m[i].maxLengh + 1;
}
}
return m[n+1].maxLengh;
}
int main()
{
int n;
cin >> n;
while (n--)
{
int max = 1;
int maxplace = 0;
int len = 0;
char str[10000];
cin >> str;
// len = strlen(str);
len = sizeof(str)-1;
Mystruct strsruct[100001];
for (int i = 0; i < len; i++)
{
strsruct[i].ch = str[i];
strsruct[i].maxLengh = 1;
}
for (int i = 0; i < len-1; i++)
{
if (max<cmp(i, strsruct, strsruct[i + 1].ch))
max = cmp(i, strsruct, strsruct[i + 1].ch);
}
cout << max << endl;
}

}
...全文
183 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 5 楼 qq_26731541 的回复:
我想知道为什么我时间超限[/quote] 我好像被封禁了,无法访问了。 你的cmp函数调用了两次,这相当于时间翻倍。 应当调用一次,结果放到变量里面,时间就会减少很多。但不知道能否通过。
自信男孩 2017-10-19
  • 打赏
  • 举报
回复
char str[10000]; 当然这个也是太大了。不要在栈上申请了。建议,在堆上(new和delete)或数据段(全局变量或静态变量)
自信男孩 2017-10-19
  • 打赏
  • 举报
回复
引用 6 楼 qq_26731541 的回复:
[quote=引用 3 楼 cfjtaishan 的回复:]
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>

using namespace std;

static char str[10001], ch[10001];

int max_inc_substr_len(char *str, int len);

int main(void)
{
    int n, i;

    cin>>n;

    if (n <= 0 || n > 20)
        return 0;

    int *len_list = new int [n];

    for (i = 0; i < n; i++)
        len_list[i] = 1;

    i = 0;
    while (i < n) {
        cin>>str;
        len_list[i] = max_inc_substr_len(str, strlen(str));
        i++;
    }

    for (i = 0; i < n; i++)
        cout<<len_list[i]<<endl;

    delete [] len_list;

    return 0;
}

int max_inc_substr_len(char *str, int len)
{
    int j = 1;

    ch[0] = str[0];
    for(int i = 1; i < len;++i)
    {
        if(str[i] > ch[j-1])
        {
            ch[j] = str[i];
            j++;
        }
        else
        {
            int t = lower_bound(ch, ch+j, str[i]) - ch;
            ch[t] = str[i];
        }
    }

    return j;
}
参考一下吧
我想知道我的代码怎么改才不会时间超限[/quote] 你的代码不单单是超时的问题,逻辑和数据运用也有问题; 结构体里的成员ch是一个字符,而你却要吧字符串赋值给ch;类型不匹配呢; 导致超时的最重要原因是Mystruct strsruct[100001];超大局部变量在栈上申请本身就会影响函数调用,因为栈空间是稀缺资源,尽量不要在栈申请大的数组
clearscout 2017-10-19
  • 打赏
  • 举报
回复
引用 3 楼 cfjtaishan 的回复:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>

using namespace std;

static char str[10001], ch[10001];

int max_inc_substr_len(char *str, int len);

int main(void)
{
    int n, i;

    cin>>n;

    if (n <= 0 || n > 20)
        return 0;

    int *len_list = new int [n];

    for (i = 0; i < n; i++)
        len_list[i] = 1;

    i = 0;
    while (i < n) {
        cin>>str;
        len_list[i] = max_inc_substr_len(str, strlen(str));
        i++;
    }

    for (i = 0; i < n; i++)
        cout<<len_list[i]<<endl;

    delete [] len_list;

    return 0;
}

int max_inc_substr_len(char *str, int len)
{
    int j = 1;

    ch[0] = str[0];
    for(int i = 1; i < len;++i)
    {
        if(str[i] > ch[j-1])
        {
            ch[j] = str[i];
            j++;
        }
        else
        {
            int t = lower_bound(ch, ch+j, str[i]) - ch;
            ch[t] = str[i];
        }
    }

    return j;
}
参考一下吧
我想知道我的代码怎么改才不会时间超限
clearscout 2017-10-19
  • 打赏
  • 举报
回复
引用 4 楼 zjq9931 的回复:
[quote=引用 2 楼 qq_26731541 的回复:] 南阳理工acm http://acm.nyist.net/JudgeOnline/problem.php?pid=17
看看能看懂吗,哪里不懂可以问。

#include<iostream>  
#include <string>
using namespace std;
int main()
{
    int n;
    cin >> n;
    while (n--)
    {
        string str;
        int count = 1;
		cin >> str;
        int a[200];
        a[0] = -999;
        for (int i = 0;i < str.length();i++)
        {
            for (int j = count - 1;j >= 0;j--)
            {
                if ((int)str[i] > a[j])
                {
                    a[j + 1] = str[i];
                    if (j + 1 == count) count++;
                    break;
                }
            }
        }
        cout << count - 1 << endl;
    }
    return 0;
}
[/quote]我想知道为什么我时间超限
  • 打赏
  • 举报
回复
引用 2 楼 qq_26731541 的回复:
南阳理工acm http://acm.nyist.net/JudgeOnline/problem.php?pid=17
看看能看懂吗,哪里不懂可以问。

#include<iostream>  
#include <string>
using namespace std;
int main()
{
    int n;
    cin >> n;
    while (n--)
    {
        string str;
        int count = 1;
		cin >> str;
        int a[200];
        a[0] = -999;
        for (int i = 0;i < str.length();i++)
        {
            for (int j = count - 1;j >= 0;j--)
            {
                if ((int)str[i] > a[j])
                {
                    a[j + 1] = str[i];
                    if (j + 1 == count) count++;
                    break;
                }
            }
        }
        cout << count - 1 << endl;
    }
    return 0;
}
自信男孩 2017-10-19
  • 打赏
  • 举报
回复
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>

using namespace std;

static char str[10001], ch[10001];

int max_inc_substr_len(char *str, int len);

int main(void)
{
    int n, i;

    cin>>n;

    if (n <= 0 || n > 20)
        return 0;

    int *len_list = new int [n];

    for (i = 0; i < n; i++)
        len_list[i] = 1;

    i = 0;
    while (i < n) {
        cin>>str;
        len_list[i] = max_inc_substr_len(str, strlen(str));
        i++;
    }

    for (i = 0; i < n; i++)
        cout<<len_list[i]<<endl;

    delete [] len_list;

    return 0;
}

int max_inc_substr_len(char *str, int len)
{
    int j = 1;

    ch[0] = str[0];
    for(int i = 1; i < len;++i)
    {
        if(str[i] > ch[j-1])
        {
            ch[j] = str[i];
            j++;
        }
        else
        {
            int t = lower_bound(ch, ch+j, str[i]) - ch;
            ch[t] = str[i];
        }
    }

    return j;
}
参考一下吧
clearscout 2017-10-19
  • 打赏
  • 举报
回复
南阳理工acm http://acm.nyist.net/JudgeOnline/problem.php?pid=17
  • 打赏
  • 举报
回复
题目地址给过来一下,通过了之后发你。

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧