当前位置:编程学习 > C/C++ >>

Codeforces Round #208 (Div. 2) B. Dima and Text Messages

B. Dima and Text Messages
 
思路很简单,字符串匹配,顺序扫一遍,o(n)的算法,居然TLE~~~~
 
原因居然是strlen!!!!
 
难道执行100000 strlen也算超时!!!! 受教了!!!
 
TLE代码如下:
 
 
 
#include<algorithm>  
#include<iostream>  
#include<stdio.h>  
#include<stdlib.h>  
#include<cstring>  
#include<cmath>  
using namespace std;  
#define N 500005  
#define eps 1e-8  
  
char words[N];  
char key[N];  
  
int solve(){  
    int i=0;  
    int len=strlen(key);  
    for(int j=0;j<len;j++)  
    if(key[j]==words[i])i++;  
    return i==strlen(words);  
}  
  
int main()  
{  
    int n,len;  
    scanf("%d",&n);  
    words[0]='<';  
    words[1]='3';  
    len=2;  
    for(int i=0;i<n;i++){  
    scanf("%s",words+len);  
    len=strlen(words);  
    words[len++]='<';  
    words[len++]='3';  
    }  
    words[len]=0;  
    scanf("%s",key);  
  
    if(solve())printf("yes\n");  
    else printf("no\n");  
//system("pause");  
    return 0;  
}  

 

 
修改strlen 之后,居然就过了~~~~~~
 
 
for(int i=0;i<n;i++){  
    scanf("%s",word);  
    for(int j=0;word[j];j++)  
        words[len++]=word[j];  
    words[len++]='<';  
    words[len++]='3';  
}  

 

 
看来,以后用库函数,要慎重!!!
 
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,