1. 程式人生 > >[題] [NOIP2011] 統計單詞數

[題] [NOIP2011] 統計單詞數

不用string  用string 方法對比

//1.1 不用string 純笨辦法

#include<iostream>

#include<cstdio>

#include<string>

#include<cstring>

using namespace std;

char word[11];

char str[1000001];//如果宣告為區域性變數,需memset

int pos=-1;//最早位置

bool check(int ps,int pa) {//ps字元串游標    pa單詞遊標

       char t1,t2;

       while(str[ps]!='\0'&&word[pa]!='\0') {

              t1=min(str[ps],word[pa]);

              t2=max(str[ps],word[pa]);

              if(t1==t2||t1+32==t2) {//處理大小寫

                     ps++;

                     pa++;

              } else {

                     return false;

              }

       }

       if((str[ps]==' '||str[ps]=='\0')&&word[pa]=='\0') return true;

       else return false;

}

int main() {

       freopen("stat.in","r",stdin);

       freopen("stat.out","w",stdout);

       cin>>word;

       cin.get();   //過濾掉上一個cin最後的空格

       cin.getline(str,1000000,'\n');   //第三個引數可不寫

       int len2=strlen(str);  //需標頭檔案cstring

       int len1=strlen(word);

       int ans=0;

       int p;

       for(int i=0; i<len2; i++) {

              if(i==0) {

                     if(check(0,0)) {

                            if(pos==-1) pos=0;

                            ans++;

                     }

              } else if(str[i]==' ') {

                     if(check(i+1,0)) {

                            if(pos==-1) pos=i+1;

                            ans++;

                     }

              }

       }

       if(pos!=-1)     cout<<ans<<" "<<pos<<endl;

       else cout<<pos<<endl;

       return 0;

}

//1.2先處理大小寫問題,其他一樣沒變

#include<iostream>

#include<cstdio>

#include<string>

#include<cstring>

using namespace std;

//不用string 純笨辦法

char word[11];

char str[1000001];//如果宣告為區域性變數,需memset

int pos=-1;//最早位置

void ftolower(char a[],int size){//轉換大小寫

       for(int i=0;i<size;i++){

              a[i]=tolower(a[i]);//cctype

       }

}

bool check(int ps,int pa) {//ps字元串游標    pa單詞遊標

       while(str[ps]!='\0'&&word[pa]!='\0') {

              if(str[ps]==word[pa]) {//處理大小寫

                     ps++;

                     pa++;

              } else {

                     return false;

              }

       }

       if((str[ps]==' '||str[ps]=='\0')&&word[pa]=='\0') return true;

       else return false;

}

int main() {

       freopen("stat.in","r",stdin);

       freopen("stat.out","w",stdout);

       cin>>word;

       cin.get();   //過濾掉上一個cin最後的空格

       cin.getline(str,1000000,'\n');   //第三個引數可不寫

       int len2=strlen(str);  //需標頭檔案cstring

       int len1=strlen(word);

       ftolower(word,len1);ftolower(str,len2);

       int ans=0;

       for(int i=0; i<len2; i++) {

              if(i==0) {

                     if(check(0,0)) {

                            if(pos==-1) pos=0;

                            ans++;

                     }

              } else if(str[i]==' ') {

                     if(check(i+1,0)) {

                            if(pos==-1) pos=i+1;

                            ans++;

                     }

              }

       }

       if(pos!=-1)     cout<<ans<<" "<<pos<<endl;

       else cout<<pos<<endl;

       return 0;

}

//1.3 string substr string對比

#include<iostream>

#include<cstdio>

#include<string>

#include<cstring>

using namespace std;

//用string substr string對比

string word,text;

int firstp=-1;

int main() {

       freopen("stat.in","r",stdin);

       freopen("stat.out","w",stdout);

       cin>>word;

       cin.get();   //過濾掉上一個cin最後的空格

       getline(cin,text);//該寫法不用寫大小 另一寫法cin.getline()的物件得是char陣列

       for(int i=0;i<word.length();i++){

              word[i]=tolower(word[i]);

       }

       for(int j=0;j<text.length();j++){

              text[j]=tolower(text[j]);

       }

       int p1=0,ans=0; string strn;

       for(int i=0;i<=text.length();i++){

              if(text[i]==' '||text[i]=='\0'){//

                     strn=text.substr(p1,i-p1);

                     //cout<<word<<" "<<strn<<endl;

                     if(word==strn){//  可直接對比

                            if(firstp==-1){

                                   firstp=p1;

                            }

                            ans++;

                     }

                     p1=i+1;

              }

       }

       if(firstp!=-1)   cout<<ans<<" "<<firstp;

       else cout<<firstp;

       fclose(stdin);fclose(stdout);

       return 0;

}