首页
社区
课程
招聘
[求助]请教一个有关基础C++编程标准输入输出的问题。
发表于: 2006-11-23 16:52 4131

[求助]请教一个有关基础C++编程标准输入输出的问题。

2006-11-23 16:52
4131
如果要在C++中一次读入有100000个字符的字符串,该如何实现。
char str[100005];
cin>>str;//或cin.get(str,100000);
能否实现?

如不能。请给出一个实现方法。
如能,请帮忙看一下我的程序的错误到底在何处,Problem  E是问题,//program E是我解决此问题的程序。
谢谢!

Problem  E
Description
Given a string consisting of brackets of two types, find its longest substring that is a regular brackets sequence. A regular brackets sequence is defined as below:
1.    an empty sequence is a regular brackets sequence;
2.    if S is a regular brackets sequence, (S), [S] are both regular brackets sequences.
3.    if S1 and S2 are regular brackets sequences, the concatenation of S1 and S2 is a regular brackets sequence.
For instance, [()], (([[([])]])), ()[()] are all regular brackets sequences. Note that a substring is a continuous section of the original string.
Input
There are multiple test cases in the input. The first line of the input contains a single integer N (0 < N < 20), which is the number of the test cases. Each test case contains a string containing only characters ‘(’ , ‘)’ , ‘[’ and ‘]’ . The length of the string does not exceed 100,000.
Output
For the given string in each test case, output the length of its longest substring that is a regular brackets sequence in a single line.
Note that a brute force method will exceed the time limit.
Sample Input
3
([(][()]]()
([)]
()[]()[])()[]))[](((())))[](((())))
Sample Output
4
0
20

//program E

#include<iostream>

using namespace std;

long kuohao(char str[]);

int main()
{
    int str_num;
    cin>>str_num;
   
    char **str=new char * [str_num];
   
    int i=0;
   
    for(i=0;i<str_num;i++)
    {
        cin.get();
        str[i]=new char[100005];        
        cin.get(str[i],100000,'\n');
        //cout<<str[5000];
    }
    cin.get();

    for(i=0;i<str_num;i++)
        cout<<kuohao(str[i])<<endl;

    for(i=0;i<str_num;i++)
        delete [] str[i];
    delete [] str;
    return 0;
}

long kuohao(char str[])
{
    long num[100005]={0};
    if(str[0]=='\0'||str[1]=='\0')
        return 0;
    if(str[0]=='('&&str[1]==')'||str[0]=='['&&str[1]==']')
        num[1]=2;
    long i=2;
    while(str[i]!='\0')
    {
        if(str[i]==')'&&str[i-num[i-1]-1]=='('||str[i]==']'&&str[i-num[i-1]-1]=='[')
            num[i++]=num[i-1]+2+(i-2-num[i-1]>=0?num[i-2-num[i-1]]:0);
        else
            num[i++]=0;
    }
//cout<<i<<"  ";cout<<str[40000]<<"  ";
    long max=0;
    for(int j=1;j<i;j++)
        if(num[j]>max)
            max=num[j];
    return max;
}
以上是我要解的题和我写的程序,是我校今年九月份的ACM预选题,测试数据的前5组长度较小,我的程序输出与标准答案相同,后面的测试数据都是上万长度的,我的输出就与答案相差甚大,答案为几万的我的程序只有一两千,于是我在程序中那样插了几个输出,输出的i为4094,str[40000]、str[5000]为?,如结果发现在数组较长时,只有前4094位有效。各位高手可否给一个解决的方法,能把这道题做出来,谢谢。
测试数据共15组,598KB,太大了,我就不上传了。

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (1)
雪    币: 150
活跃值: (116)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
2
10W太大了吧,写到文件中,从文件读取吧
2006-11-29 17:44
0
游客
登录 | 注册 方可回帖
返回
//