【HDOJ】1648 Keywords

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     
时间: 2024-10-06 21:21:57

【HDOJ】1648 Keywords的相关文章

【HDOJ】2222 Keywords Search

AC自动机基础题. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <queue> 6 using namespace std; 7 8 #define MAXL 1000005 9 #define TRIEN 26 10 11 char des[MAXL], src[55]; 12 13 typedef

【HDOJ】4956 Poor Hanamichi

基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. 1 #include <cstdio> 2 3 int f(__int64 x) { 4 int i, sum; 5 6 i = sum = 0; 7 while (x) { 8 if (i & 1) 9 sum -= x%10; 10 else 11 sum += x%10; 12 ++i; 13 x/=10; 14 } 15 return sum; 16 } 17 18 int main() { 1

【HDOJ】1099 Lottery

题意超难懂,实则一道概率论的题目.求P(n).P(n) = n*(1+1/2+1/3+1/4+...+1/n).结果如果可以除尽则表示为整数,否则表示为假分数. 1 #include <cstdio> 2 #include <cstring> 3 4 #define MAXN 25 5 6 __int64 buf[MAXN]; 7 8 __int64 gcd(__int64 a, __int64 b) { 9 if (b == 0) return a; 10 else return

【HDOJ】2844 Coins

完全背包. 1 #include <stdio.h> 2 #include <string.h> 3 4 int a[105], c[105]; 5 int n, m; 6 int dp[100005]; 7 8 int mymax(int a, int b) { 9 return a>b ? a:b; 10 } 11 12 void CompletePack(int c) { 13 int i; 14 15 for (i=c; i<=m; ++i) 16 dp[i]

【HDOJ】3509 Buge&#39;s Fibonacci Number Problem

快速矩阵幂,系数矩阵由多个二项分布组成.第1列是(0,(a+b)^k)第2列是(0,(a+b)^(k-1),0)第3列是(0,(a+b)^(k-2),0,0)以此类推. 1 /* 3509 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #incl

【HDOJ】1818 It&#39;s not a Bug, It&#39;s a Feature!

状态压缩+优先级bfs. 1 /* 1818 */ 2 #include <iostream> 3 #include <queue> 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <algorithm> 8 using namespace std; 9 10 #define MAXM 105 11 12 typedef struct {

【HDOJ】2424 Gary&#39;s Calculator

大数乘法加法,直接java A了. 1 import java.util.Scanner; 2 import java.math.BigInteger; 3 4 public class Main { 5 public static void main(String[] args) { 6 Scanner cin = new Scanner(System.in); 7 int n; 8 int i, j, k, tmp; 9 int top; 10 boolean flag; 11 int t

【HDOJ】2425 Hiking Trip

优先级队列+BFS. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 7 #define MAXN 25 8 9 typedef struct node_st { 10 int x, y, t; 11 node_st() {} 12 node_st(int xx, int yy, int tt)

【HDOJ】1686 Oulipo

kmp算法. 1 #include <cstdio> 2 #include <cstring> 3 4 char src[10005], des[1000005]; 5 int next[10005], total; 6 7 void kmp(char des[], char src[]){ 8 int ld = strlen(des); 9 int ls = strlen(src); 10 int i, j; 11 12 total = i = j = 0; 13 while (