題目來源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=111
題意:
P:間距 、Keywords_1、Keywords_2.....、Keywords_n
T:各種標題!@#$%^^&*((()
將文章T中的單字倆倆配一對,若兩個Keywords中間所格的字母<=P給的間距,則符合並輸出T的編號
胡言亂語:
程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<cstring> | |
#include<map> | |
#include<cstdlib> | |
#include<string> | |
#include<cstdio> | |
using namespace std; | |
typedef pair<string,string> P; | |
map<P ,int> table[50]; | |
map<P ,int>::iterator it; | |
int store[500] ,store2[500] ; | |
bool find_keywords(P key,int i,int j){ | |
it = table[i].find(key); | |
if(it!=table[i].end() && it->second >= j) return true; | |
else return false; | |
} | |
string ss[251]; | |
void save(int k,int u,int x){ | |
P store_keywords; | |
for(int i=0;i<k-1;i++){ | |
for(int j=i+1;j<k;j++){ | |
store_keywords = make_pair(ss[i],ss[j]); | |
table[u][store_keywords] = x; | |
store_keywords = make_pair(ss[j],ss[i]); | |
table[u][store_keywords] = x; | |
} | |
} | |
} | |
int main(){ | |
//x->threshold ;u->the number of P ;t->the number of T | |
int x,u = 0,t = 0,t1,k ; | |
char ch; | |
//store_T->store TXT of T ;str->T組成單字使用 | |
string store_T[251][1000],str; | |
//make_pair of keywords | |
P store_keywords; | |
while(scanf("%c: ",&ch))//P or T | |
{ | |
if(ch=='P'){ | |
u++;k = 0; | |
table[u].clear(); | |
scanf("%d ",&x);//threshold | |
store[u] = x;//將p的最大threshold存起來 | |
char *p, str1[5000] ; | |
cin.getline(str1,5000); | |
p = strtok(str1, " "); | |
while (p != NULL) { | |
ss[k] = p; | |
k++; | |
p = strtok(NULL, " "); | |
} | |
save(k,u,x); | |
} | |
if(ch=='T'){ | |
t++;t1 = 0; | |
str = ""; | |
while(cin.get(ch)){ | |
if(ch>=97&&ch<=97+26) | |
str=str+ch; | |
if(ch>=65&&ch<=(65+26)){ | |
ch+=32; | |
str=str+ch; | |
} | |
if(ch==32||ch=='\t') { | |
if(str=="") continue; | |
t1++; | |
store_T[t][t1] = str; | |
str = ""; | |
} | |
if(ch=='|'){ | |
if(str=="") break; | |
t1++; | |
store_T[t][t1] = str; | |
break; | |
} | |
} | |
store2[t] = t1; | |
} | |
if(ch=='#') | |
break; | |
} | |
bool flag,flag2; | |
//從P1開始找配對 | |
for(int i=1;i<=u;i++){ | |
//從T下手 | |
flag = false; | |
printf("%d: ",i); | |
for(int j=1;j<=t;j++){ | |
//倆倆配一組 | |
flag2 = false; | |
for(int m=0;m<=store[i];m++){ | |
for(int n=1;(n+m+1)<=store2[j];n++){ | |
store_keywords = make_pair(store_T[j][n],store_T[j][n+m+1]); | |
if(find_keywords(store_keywords,i,m)){ | |
flag2=true; | |
} | |
} | |
} | |
if(flag2){ | |
if(flag) printf(",%d",j); | |
else {flag = true;printf("%d",j);} | |
} | |
} | |
printf("\n"); | |
} | |
return 0; | |
} |
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。