一天蒜頭君得到 n個字符串 si,每個字符串的長度都不超過 10。
蒜頭君在想,在這 n 個字符串中,以 si 爲後綴的字符串有多少個呢?
輸入格式
第一行輸入一個整數 n。
接下來 n 行,每行輸入一個字符串 si。
輸出格式
輸出 n 個整數,第 i個整數表示以 si 爲後綴的字符串的個數。
數據範圍
對於 50% 的數據,1<=n <=10^3。
對於 100% 的數據,1<=n <=10^5。
所有的字符串僅由小寫字母組成。
樣例輸入
3
ba
a
aba
樣例輸出
2
3
1
學了一下hash,雖然這題好像用hash挺多餘(……)不管了就當學習hash了((
題意就是統計以 si 爲後綴的字符串的個數
#include <iostream>
#include <string>
#include <map>
#include <cstring>
#include <algorithm>
#define ull unsigned long long
using namespace std;
ull base=131;
int prime=233317;
ull mod=212370440130137957ll;
ull h[100010]={0};
ull Hash(string str){//將字符串轉換成對應整數
int len=str.length(),i;
ull ans=0;
for(i=0;i<len;i++){
ans=(ans*base+(ull)str[i])%mod+prime;
}
return ans;
}
int main(){
ios::sync_with_stdio(false);
map <ull,int> mp;
int n,i,j;
cin>>n;
string s,t;
for(i=0;i<n;i++){
cin>>s;
reverse(s.begin(),s.end());
for(j=1;j<=s.length();j++){
t=s.substr(0,j);
mp[Hash(t)]++;
}
h[i]=Hash(s);
}
for(i=0;i<n;i++){
cout<<mp[h[i]]<<endl;
}
return 0;
}