1. 程式人生 > >P2264 情書(字符串hash90分)

P2264 情書(字符串hash90分)

() fine 標準 string using turn sizeof love spa

題目背景

一封好的情書需要撰寫人全身心的投入。lin_toto同學看上了可愛的卡速米想對她表白,但卻不知道自己寫的情書是否能感動她,現在他帶著情書請你來幫助他。

題目描述

為了幫助lin_toto,我們定義一個量化情書好壞的標準感動值。判斷感動值的方法如下:

1.在情書的一句話中若含有給定詞匯列表中的特定單詞,則感動值加1,但每一單詞在同一句話中出現多次感動值不疊加,不同單詞不受影響。保證輸入的單詞不重復。

2.每句話以英文句號定界。

3.全文不區分大小寫。

輸入輸出格式

輸入格式:

第一行包含一個數字n,表示導致感動值提升的詞匯列表中單詞的數量,隨後n行是給定單詞,每行一個。保證單詞只包含英文字母。

最後一行為情書正文,保證只包含以下幾種字符: 英文字母、數字、空格、英文逗號、英文句號。

輸出格式:

一個數字g,表示情書帶來的感動值。

輸入輸出樣例

輸入樣例#1:
3
love
so
much
I love you so much.
輸出樣例#1:
3

說明

對於所有的數據,保證1 ≤ n,m,k ≤ 100,每個單詞不超過50字符,全文不超過1000字符。

按理說字符串hash應該能過,

但是就是90分,,,

 1 #include<iostream>
 2 #include<cstdio>
 3
#include<cstring> 4 #include<algorithm> 5 #include<set> 6 #define ull unsigned long long 7 using namespace std; 8 const int MAXN=4001; 9 int seed=11; 10 inline void read(int &n) 11 { 12 char c=getchar();bool flag=0;n=0; 13 while(c<0||c>9) c==-
?flag=1,c=getchar():c=getchar(); 14 while(c>=0&&c<=9) n=n*10+(c-48),c=getchar();if(flag==1)n=-n; 15 } 16 int n; 17 int ans=0; 18 ull ha[MAXN];// 每一個單詞的hash 19 char s[MAXN]; 20 char now[MAXN]; 21 int tot=0; 22 ull Po[MAXN]; 23 ull a[MAXN]; 24 int vis[MAXN];// 判斷當前單詞是否用過 25 int l[MAXN];// 每一個單詞的長度 26 int gethash(int l,int r) 27 { 28 return a[r]-Po[r-l+1]*a[l-1]; 29 } 30 void calc() 31 { 32 a[0]=1; 33 for(int i=1;i<=tot;i++) 34 a[i]=a[i-1]*seed+now[i];a[0]=0; 35 for(int j=1;j<=n;j++)//枚舉每一個單詞 36 if((gethash(1,tot))==ha[j]&&vis[j]==0) 37 vis[j]=1,ans++; 38 39 } 40 int main() 41 { 42 read(n); 43 Po[0]=1; 44 for(int i=1;i<=n;i++) 45 Po[i]=Po[i-1]*seed; 46 for(int i=1;i<=n;i++) ha[i]=1; 47 for(int i=1;i<=n;i++) 48 { 49 scanf("%s",s+1); 50 l[i]=strlen(s+1); 51 for(int j=1;j<=l[i];j++) if(s[j]>=a&&s[j]<=z) s[j]=s[j]+A-a; 52 for(int j=1;j<=l[i];j++) 53 ha[i]=ha[i]*seed+s[j];// 每一個單詞的hash 54 } 55 char c=getchar(); 56 while(scanf("%c",&c)==1) 57 { 58 if((c>=a&&c<=z)||(c>=A&&c<=Z)) 59 { 60 if(c>=a&&c<=z) 61 c=c+A-a; 62 now[++tot]=c; 63 } 64 else 65 { 66 calc(); 67 memset(a,0,sizeof(a)); 68 if(c==.) 69 memset(vis,0,sizeof(vis)); 70 tot=0; 71 } 72 } 73 74 printf("%d",ans); 75 return 0; 76 }

P2264 情書(字符串hash90分)