返回信息流步步是坎啊,又来弱弱的求助。。下面的代码提交耗时3000ms,哪里如此耗时呢???
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
struct pythonList
{
char name[ 15 ];
vector<int> array;
};
int main()
{
int T;
scanf( "%d", &T );
while( T-- )
{
int N;
scanf( "%d", &N );
pythonList list[ 100 ];
int listNum = 0, curId = 0;
while( N-- )
{
fflush( stdin );
char s[ 100 ];
char tmpS[ 15 ];
memset( tmpS, 0, sizeof( tmpS ) );
int tmpSPos = 0;
scanf( "%[^\n]", s );
int len = strlen( s );
for( int i = 0; i < len; i++ )
{
if( s[ i ] == '=' )
{
tmpS[ tmpSPos ] = '\0';
if( !strcmp( list[ curId ].name, tmpS ) )
{
list[ curId ].array.clear();
}
else
{
curId = listNum;
strcpy( list[ listNum++ ].name, tmpS );
memset( tmpS, 0, sizeof( tmpS ) );
}
break;
}
else if( s[ i ] == '.' && s[ i + 1 ] == 'a' )
{
int sum = 0;
while( s[ i + 8 ] != ')' )
{
sum = sum * 10 + ( s[ i + 8 ] - '0' );
i++;
}
list[ curId ].array.push_back( sum );
break;
}
else if( s[ i ] == '.' && s[ i + 1 ] == 's' )
{
sort( list[ curId ].array.begin(), list[ curId].array.end() );
break;
}
else if( s[ i ] == '[' )
{
int num = 0;
while( s[ i + 1 ] != ']' )
{
num = num * 10 + ( s[ i + 1 ] - '0' );
i++;
}
if( num >= list[ curId ].array.size() )
printf( "ERROE\n" );
else
printf( "%d\n", list[ curId ].array[ num ] );
break;
}
tmpS[ tmpSPos++ ] = s[ i ];
}
}
}
return 0;
}
这是一条镜像帖。来源:北邮人论坛 / acm-icpc / #88954同步于 2016/3/1
该镜像源已超过 30 天没有更新,可能在源站已被删除。
ACM_ICPC机器人发帖
[求助] bupt oj 273. 这代码很耗时么 为啥TLE。。
yyss1230
2016/3/1镜像同步3 回复
订阅后,新回复会通过你的通知中心匿名送达。
3 条回复
代码提交超时一般是哪出现无限循环了吧
你可以本地自己多写几个样例调试测试一下
给你我的代码供参考
/*
USER_ID: test#yingzinanfei
PROBLEM: 273
SUBMISSION_TIME: 2015-03-26 13:41:34
*/
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
int t;
while(scanf("%d", &t) != EOF){
for(int a = 1; a <= t; a++){
int n;
scanf("%d", &n);
//getchar();
vector<int> Q[100];
char name[100][100];
int size = 0;
for(int i = 1; i <= n; i++){
char sName[100];
getchar();
scanf("%[^=.[]", sName);
int idx = -1;
for(int j = 0; j < 100; j++){
if(strcmp(name[j], sName) == 0){
idx = j;
}
}
char next;
scanf("%c", &next);
if(next == '.'){
char inst[10];
scanf("%[^(]", inst);
if(strcmp(inst, "sort") == 0){
char jump[10];
scanf("%s", jump);
sort(Q[idx].begin(), Q[idx].end());
}
else if(strcmp(inst, "append") == 0){
char jump;
scanf("%c", &jump);
int x;
scanf("%d", &x);
Q[idx].push_back(x);
scanf("%c", &jump);
//getchar();
}
}
else if (next == '='){
if(idx == - 1) {
idx = size++;
int j;
for(j = 0; sName[j] != 0; j++){
name[idx][j] = sName[j];
}
name[idx][j] = 0;
}
while(Q[idx].empty() != true) Q[idx].pop_back();
char jump[10];
scanf("%s", jump);
}
else if (next == '['){
int ix;
scanf("%d", &ix);
if(ix >= Q[idx].size()){
printf("ERROR\n");
}
else printf("%d\n", Q[idx].at(ix));
char jump[10];
scanf("%s", jump);
}
}
}
}
return 0;
}
【 在 yyss1230 的大作中提到: 】
: 步步是坎啊,又来弱弱的求助。。下面的代码提交耗时3000ms,哪里如此耗时呢???
: #include<stdio.h>
: #include<string.h>
: ...................
哦哦 膜拜膜拜。。 看大神代码能学到不少咧
【 在 chenxiansf 的大作中提到: 】
: 楼主加油
: 代码可以选中后加代码高亮
: 话说我当时做的时候为什么一点不爱用vector。。。