[Trie] Luogu P2580 于是他错误的点名开始了
生活随笔
收集整理的這篇文章主要介紹了
[Trie] Luogu P2580 于是他错误的点名开始了
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述
這之后校長任命你為特派探員,每天記錄他的點名。校長會提供化學競賽學生的人數和名單,而你需要告訴校長他有沒有點錯名。(為什么不直接不讓他玩爐石。)
輸入輸出格式
輸入格式:
第一行一個整數 n,表示班上人數。接下來 n 行,每行一個字符串表示其名字(互不相同,且只含小寫字母,長度不超過 50)。第 n+2 行一個整數 m,表示教練報的名字。接下來 m 行,每行一個字符串表示教練報的名字(只含小寫字母,且長度不超過 50)。
輸出格式:
對于每個教練報的名字,輸出一行。如果該名字正確且是第一次出現,輸出“OK”,如果該名字錯誤,輸出“WRONG”,如果該名字正確但不是第一次出現,輸出“REPEAT”。(均不加引號)
輸入輸出樣例
輸入樣例#1:5 a b c ad acd 3 a a e 輸出樣例#1:
OK REPEAT WRONG
說明
對于 40%的數據,n≤1000,m≤2000;
對于 70%的數據,n≤10000,m≤20000;
對于 100%的數據, n≤10000,m≤100000。
?
題解
- 這題應該是比較裸的一棵trie
- 插入、搜索,再加多一個cnt統計有沒有找過
代碼
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 using namespace std; 5 int n,m,v[800000],end[800000],tot,trie[800000][40]; 6 char ch[60]; 7 void insert(char *str) 8 { 9 int len=strlen(str),p=0; 10 for (int i=0;i<len;i++) 11 { 12 int ch=str[i]-'a'; 13 if (trie[p][ch]==0) trie[p][ch]=++tot; 14 p=trie[p][ch]; 15 } 16 end[p]=true; 17 } 18 void search(char *str) 19 { 20 int len=strlen(str),p=0; 21 for (int i=0;i<len;i++) 22 { 23 p=trie[p][str[i]-'a']; 24 if (p==0) 25 { 26 cout<<"WRONG"<<endl; 27 return; 28 } 29 } 30 if (end[p]==false) 31 { 32 cout<<"WRONG"<<endl; 33 return; 34 } 35 if (v[p]!=0) 36 { 37 cout<<"REPEAT"<<endl; 38 return; 39 } 40 if (v[p]==0) 41 { 42 cout<<"OK"<<endl; 43 v[p]=1; 44 } 45 46 } 47 int main() 48 { 49 scanf("%d",&n); 50 for (int i=1;i<=n;i++) 51 { 52 scanf("%s",ch); 53 insert(ch); 54 } 55 scanf("%d",&m); 56 for (int i=1;i<=m;i++) 57 { 58 scanf("%s",ch); 59 search(ch); 60 } 61 return 0; 62 }?
轉載于:https://www.cnblogs.com/Comfortable/p/8796424.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的[Trie] Luogu P2580 于是他错误的点名开始了的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSS3特效之转化(transform)
- 下一篇: 2018华南理工大学程序设计竞赛 H-对