zoukankan      html  css  js  c++  java
  • 洛谷【P1308】统计单词数

    思路:逐个找出单词进行比对

    笔者遇坑处:下方代码28行处判断条件未考虑到字符串末尾 ''。

          只判断空格,会导致部分情况下跳不出循环。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 char text[1000005], check[15];
     6 int len_text,len_check;
     7 int flag_loc,cnt;
     8 int main(){
     9     memset(check,0,sizeof(check));
    10     memset(text,0,sizeof(text));
    11     gets(check);
    12     gets(text);
    13     len_text=strlen(text);
    14     len_check=strlen(check);
    15     for(int q=0; q<len_text; q++){
    16         if(text[q]>='A' && text[q]<='Z')    text[q]+='a'-'A';
    17     }
    18     for(int q=0; q<=len_check; q++){
    19         if(check[q]>='A' && check[q]<='Z')    check[q]+='a'-'A';
    20     }              //对大小写进行统一
    21 //    puts(text);
    22 //    puts(check);
    23     int loc_text=0,flag;
    24     char temp[30];
    25     while(loc_text <= len_text){
    26         memset(temp,0,sizeof(temp));
    27         int loc_temp=0;
    28         while(text[loc_text]!=' ' && text[loc_text]!=''){
    29             temp[loc_temp]=text[loc_text];
    30             loc_text++;
    31             loc_temp++;
    32         }          //逐个找出单词进行比对
    33         flag=strcmp(temp,check);
    34         if(flag==0 && cnt==0)    flag_loc=loc_text-len_check;    //寻找首次位置
    35         if(flag==0)    cnt++;
    36         loc_text++;
    37     }
    38     if(cnt==0)        cout<<-1;
    39     else            cout<<cnt<<' '<<flag_loc;
    40     return 0;
    41 }
    我是这耀眼的瞬间,是划过天边的刹那火焰。
  • 相关阅读:
    1058
    light oj 1067 费马小定理求逆元
    HDU 1022
    贪心
    HDU 4994 博弈。
    HDU 5234 背包。
    CodeForces 327B 水题。
    vue的$nextTick使用总结,this.$refs为undefined的解决办法,element Ui的树形控件setCheckedKeys方法无法使用
    Object.assign的用法
    react + antd 实现打印功能(踩了不少坑)
  • 原文地址:https://www.cnblogs.com/Rane/p/9143970.html
Copyright © 2011-2022 走看看