码迷,mamicode.com
首页 > 其他好文 > 详细

【HDOJ】1648 Keywords

时间:2016-01-16 20:53:52      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:

PE的注意,如果没有满足条件的不输出空格。
简单模拟,暴力解。

  1 /*  */
  2 #include <iostream>
  3 #include <sstream>
  4 #include <string>
  5 #include <map>
  6 #include <queue>
  7 #include <set>
  8 #include <stack>
  9 #include <vector>
 10 #include <deque>
 11 #include <bitset>
 12 #include <algorithm>
 13 #include <cstdio>
 14 #include <cmath>
 15 #include <ctime>
 16 #include <cstring>
 17 #include <climits>
 18 #include <cctype>
 19 #include <cassert>
 20 #include <functional>
 21 #include <iterator>
 22 #include <iomanip>
 23 using namespace std;
 24 //#pragma comment(linker,"/STACK:102400000,1024000")
 25 
 26 #define sti                set<int>
 27 #define stpii            set<pair<int, int> >
 28 #define mpii            map<int,int>
 29 #define vi                vector<int>
 30 #define pii                pair<int,int>
 31 #define vpii            vector<pair<int,int> >
 32 #define rep(i, a, n)     for (int i=a;i<n;++i)
 33 #define per(i, a, n)     for (int i=n-1;i>=a;--i)
 34 #define clr                clear
 35 #define pb                 push_back
 36 #define mp                 make_pair
 37 #define fir                first
 38 #define sec                second
 39 #define all(x)             (x).begin(),(x).end()
 40 #define SZ(x)             ((int)(x).size())
 41 #define lson            l, mid, rt<<1
 42 #define rson            mid+1, r, rt<<1|1
 43 
 44 // #define DEBUG
 45 
 46 const int maxp = 55;
 47 const int maxt = 255;
 48 const int maxl = 255;
 49 const int maxn = 305;
 50 const int INF = 0x3f3f3f3f;
 51 int M[maxn], mn;
 52 int M_[maxn], mn_;
 53 map<string,int> tb;
 54 map<string,int>::iterator iter;
 55 int wc = 0, pn = 1, tn = 1;
 56 char s[maxl];
 57 
 58 typedef struct {
 59     int l;
 60     char s[maxl];
 61 
 62     void init() {
 63         int id;
 64         int len = strlen(s);
 65         int i = 0, j;
 66 
 67         mn = 0;
 68         while (i < len) {
 69             if (isspace(s[i])) {
 70                 ++i;
 71                 continue;
 72             }
 73             j = i;
 74             while (i<len && islower(s[i]))
 75                 ++i;
 76             s[i] = \0;
 77             #ifdef DEBUG
 78                 printf("\t%s\n", s+j);
 79             #endif
 80             string str(s+j);
 81             iter = tb.find(str);
 82             if (iter == tb.end()) {
 83                 id = tb[str] = wc++;
 84             } else {
 85                 id = iter->sec;
 86             }
 87 
 88             M[mn++] = id;
 89             s[i] =  ;
 90         }
 91 
 92         M[mn] = INF;
 93     }
 94 
 95 } profile_t;
 96 
 97 typedef struct {
 98     int a[maxn];
 99     int sz;
100 } tile_t;
101 
102 profile_t P[maxp];
103 tile_t T[maxt];
104 
105 void input_profile() {
106     scanf("%d", &P[pn].l);
107     gets(P[pn].s);
108     // getchar();
109     ++pn;
110 }
111 
112 void input_tile() {
113     int l = 0, id;
114     char ch;
115 
116     #ifdef DEBUG
117         printf("%d:\n", tn);
118     #endif
119 
120     while (1) {
121         while (1) {
122             ch = getchar();
123             if (isspace(ch))
124                 continue;
125             else
126                 break;
127         }
128 
129         if (ch == |)
130             break;
131 
132         // have a word
133         l = 0;
134         while (1) {
135             if (isalpha(ch)) {
136                 if (isupper(ch))
137                     ch = ch - A + a;
138                 s[l++] = ch;
139             } else if (isspace(ch) || ch==|) {
140                 if (l) {
141                     s[l] = \0;
142                     #ifdef DEBUG
143                         printf("\t%s\n", s);
144                     #endif
145                     string str(s);
146                     iter = tb.find(str);
147                     if (iter == tb.end()) {
148                         id = tb[str] = wc++;
149                     } else {
150                         id = iter->sec;
151                     }
152                     T[tn].a[T[tn].sz++] = id;
153                     l = 0;
154                 }
155                 break;
156             }
157 
158             ch = getchar();
159         }
160 
161         if (ch == |) {
162             getchar();
163             break;
164         }
165     }
166 
167     ++tn;
168 }
169 
170 bool judge(int pid, int tid) {
171     int l = P[pid].l + 1;
172     int sz = T[tid].sz;
173 
174     // sort(M, M+mn);
175     
176     int tmp;
177     rep(i, 0, mn) {
178         rep(j, 0, mn) {
179             if (i == j)
180                 continue;
181             tmp = INF;
182             rep(ii, 0, sz) {
183                 if (T[tid].a[ii] != M[i])
184                     continue;
185                 rep(jj, 0, sz) {
186                     if (jj==ii || T[tid].a[jj]!=M[j])
187                         continue;
188                     tmp = min(tmp, abs(jj-ii));
189                 }
190             }
191 
192             if (tmp <= l)
193                 return true;
194         }
195     }
196 
197     return false;
198 }
199 
200 void solve_(int pid) {
201     int c = 0;
202 
203     printf("%d:", pid);
204 
205     P[pid].init();
206     rep(tid, 1, tn) {
207         if (judge(pid, tid)) {
208             if (c++)
209                 putchar(,);
210             else
211                 putchar( );
212             printf("%d", tid);
213         }
214     }
215 
216 
217     putchar(\n);
218 }
219 
220 void solve() {
221     rep(i, 1, pn)
222         solve_(i);
223 }
224 
225 int main() {
226     ios::sync_with_stdio(false);
227     #ifndef ONLINE_JUDGE
228         freopen("data.in", "r", stdin);
229         freopen("data.out", "w", stdout);
230     #endif
231 
232     char ch;
233 
234     while (1) {
235         ch = getchar();
236         if (ch == #)
237             break;
238         getchar();
239         if (ch == P) {
240             input_profile();
241         } else {
242             input_tile();
243         }
244     }
245 
246     solve();
247 
248     #ifndef ONLINE_JUDGE
249         printf("time = %d.\n", (int)clock());
250     #endif
251 
252     return 0;
253 }

数据发生器。

  1 import sys
  2 import string
  3 from random import randint
  4 
  5 def GenWords(n = 20):
  6     ret = []
  7     lc = list(string.lowercase)
  8     llc = len(lc) - 1
  9     for i in xrange(n):
 10         length = randint(2, 5)
 11         word = ""
 12         for j in xrange(length):
 13             idx = randint(0, llc)
 14             word += lc[idx]
 15         ret.append( word )
 16     return ret
 17 
 18     
 19 def GenWord(word):
 20     op = "!#$%^&*()023+-"
 21     lop = len(op) - 1
 22     ret = ""
 23     for i in xrange(len(word)):
 24         k = randint(0, 100)
 25         if k%4 == 0:
 26             idx = randint(0, lop)
 27             ret += op[idx]
 28         if k%4 == 1:
 29             ret += word[i].upper()
 30         else:
 31             ret += word[i]
 32     return ret
 33     
 34     
 35 def GenSpace():
 36     op = " \t"
 37     length = randint(1, 3)
 38     ret = ""
 39     for i in xrange(length):
 40         k = randint(0, 1)
 41         ret += op[k]
 42     return ret
 43     
 44     
 45 def GenTile(wordList):
 46     lw = len(wordList) - 1
 47     ret = "T: "
 48     nline = randint(1, 5)
 49     lines = []
 50     for i in xrange(nline):
 51         nword = randint(2, 10)
 52         line = ""
 53         for i in xrange(nword):
 54             line += GenSpace()
 55             idx = randint(0, lw)
 56             line += GenWord(wordList[idx])
 57         lines.append(line)
 58     ret = ret + "\n".join(lines) + "|"
 59     return ret
 60 
 61     
 62 def GenPile(wordList):
 63     lw = len(wordList) - 1
 64     ret = "P: "
 65     k = randint(0, 10)
 66     ret += " %d" % k
 67     nword = randint(2, 10)
 68     for i in xrange(nword):
 69         ret += GenSpace()
 70         idx = randint(0, lw)
 71         ret += wordList[idx]
 72     return ret
 73     
 74 
 75 def GenData(fileName):
 76     wordList = GenWords()
 77     lw = len(wordList) - 1
 78     with open(fileName, "w") as fout:
 79         pn = randint(10, 20)
 80         tn = randint(10, 30)
 81         for i in xrange(pn):
 82             line = GenPile(wordList)
 83             fout.write("%s\n" % line)
 84         for i in xrange(tn):
 85             line = GenTile(wordList)
 86             fout.write("%s\n" % line)    
 87         fout.write("#\n")
 88         
 89         
 90 def MovData(srcFileName, desFileName):
 91     with open(srcFileName, "r") as fin:
 92         lines = fin.readlines()
 93     with open(desFileName, "w") as fout:
 94         fout.write("".join(lines))
 95 
 96         
 97 def CompData():
 98     print "comp"
 99     srcFileName = "F:\Qt_prj\hdoj\data.out"
100     desFileName = "F:\workspace\cpp_hdoj\data.out"
101     srcLines = []
102     desLines = []
103     with open(srcFileName, "r") as fin:
104         srcLines = fin.readlines()
105     with open(desFileName, "r") as fin:
106         desLines = fin.readlines()
107     n = min(len(srcLines), len(desLines))-1
108     for i in xrange(n):
109         ans2 = int(desLines[i])
110         ans1 = int(srcLines[i])
111         if ans1 > ans2:
112             print "%d: wrong" % i
113 
114             
115 if __name__ == "__main__":
116     srcFileName = "F:\Qt_prj\hdoj\data.in"
117     desFileName = "F:\workspace\cpp_hdoj\data.in"
118     GenData(srcFileName)
119     MovData(srcFileName, desFileName)
120     
121     

 

【HDOJ】1648 Keywords

标签:

原文地址:http://www.cnblogs.com/bombe1013/p/5136241.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!