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

 /*  */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 // #define DEBUG const int maxp = ;
const int maxt = ;
const int maxl = ;
const int maxn = ;
const int INF = 0x3f3f3f3f;
int M[maxn], mn;
int M_[maxn], mn_;
map<string,int> tb;
map<string,int>::iterator iter;
int wc = , pn = , tn = ;
char s[maxl]; typedef struct {
int l;
char s[maxl]; void init() {
int id;
int len = strlen(s);
int i = , j; mn = ;
while (i < len) {
if (isspace(s[i])) {
++i;
continue;
}
j = i;
while (i<len && islower(s[i]))
++i;
s[i] = '\0';
#ifdef DEBUG
printf("\t%s\n", s+j);
#endif
string str(s+j);
iter = tb.find(str);
if (iter == tb.end()) {
id = tb[str] = wc++;
} else {
id = iter->sec;
} M[mn++] = id;
s[i] = ' ';
} M[mn] = INF;
} } profile_t; typedef struct {
int a[maxn];
int sz;
} tile_t; profile_t P[maxp];
tile_t T[maxt]; void input_profile() {
scanf("%d", &P[pn].l);
gets(P[pn].s);
// getchar();
++pn;
} void input_tile() {
int l = , id;
char ch; #ifdef DEBUG
printf("%d:\n", tn);
#endif while () {
while () {
ch = getchar();
if (isspace(ch))
continue;
else
break;
} if (ch == '|')
break; // have a word
l = ;
while () {
if (isalpha(ch)) {
if (isupper(ch))
ch = ch - 'A' + 'a';
s[l++] = ch;
} else if (isspace(ch) || ch=='|') {
if (l) {
s[l] = '\0';
#ifdef DEBUG
printf("\t%s\n", s);
#endif
string str(s);
iter = tb.find(str);
if (iter == tb.end()) {
id = tb[str] = wc++;
} else {
id = iter->sec;
}
T[tn].a[T[tn].sz++] = id;
l = ;
}
break;
} ch = getchar();
} if (ch == '|') {
getchar();
break;
}
} ++tn;
} bool judge(int pid, int tid) {
int l = P[pid].l + ;
int sz = T[tid].sz; // sort(M, M+mn); int tmp;
rep(i, , mn) {
rep(j, , mn) {
if (i == j)
continue;
tmp = INF;
rep(ii, , sz) {
if (T[tid].a[ii] != M[i])
continue;
rep(jj, , sz) {
if (jj==ii || T[tid].a[jj]!=M[j])
continue;
tmp = min(tmp, abs(jj-ii));
}
} if (tmp <= l)
return true;
}
} return false;
} void solve_(int pid) {
int c = ; printf("%d:", pid); P[pid].init();
rep(tid, , tn) {
if (judge(pid, tid)) {
if (c++)
putchar(',');
else
putchar(' ');
printf("%d", tid);
}
} putchar('\n');
} void solve() {
rep(i, , pn)
solve_(i);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif char ch; while () {
ch = getchar();
if (ch == '#')
break;
getchar();
if (ch == 'P') {
input_profile();
} else {
input_tile();
}
} solve(); #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

数据发生器。

 import sys
import string
from random import randint def GenWords(n = 20):
ret = []
lc = list(string.lowercase)
llc = len(lc) - 1
for i in xrange(n):
length = randint(2, 5)
word = ""
for j in xrange(length):
idx = randint(0, llc)
word += lc[idx]
ret.append( word )
return ret def GenWord(word):
op = "!#$%^&*()023+-"
lop = len(op) - 1
ret = ""
for i in xrange(len(word)):
k = randint(0, 100)
if k%4 == 0:
idx = randint(0, lop)
ret += op[idx]
if k%4 == 1:
ret += word[i].upper()
else:
ret += word[i]
return ret def GenSpace():
op = " \t"
length = randint(1, 3)
ret = ""
for i in xrange(length):
k = randint(0, 1)
ret += op[k]
return ret def GenTile(wordList):
lw = len(wordList) - 1
ret = "T: "
nline = randint(1, 5)
lines = []
for i in xrange(nline):
nword = randint(2, 10)
line = ""
for i in xrange(nword):
line += GenSpace()
idx = randint(0, lw)
line += GenWord(wordList[idx])
lines.append(line)
ret = ret + "\n".join(lines) + "|"
return ret def GenPile(wordList):
lw = len(wordList) - 1
ret = "P: "
k = randint(0, 10)
ret += " %d" % k
nword = randint(2, 10)
for i in xrange(nword):
ret += GenSpace()
idx = randint(0, lw)
ret += wordList[idx]
return ret def GenData(fileName):
wordList = GenWords()
lw = len(wordList) - 1
with open(fileName, "w") as fout:
pn = randint(10, 20)
tn = randint(10, 30)
for i in xrange(pn):
line = GenPile(wordList)
fout.write("%s\n" % line)
for i in xrange(tn):
line = GenTile(wordList)
fout.write("%s\n" % line)
fout.write("#\n") def MovData(srcFileName, desFileName):
with open(srcFileName, "r") as fin:
lines = fin.readlines()
with open(desFileName, "w") as fout:
fout.write("".join(lines)) def CompData():
print "comp"
srcFileName = "F:\Qt_prj\hdoj\data.out"
desFileName = "F:\workspace\cpp_hdoj\data.out"
srcLines = []
desLines = []
with open(srcFileName, "r") as fin:
srcLines = fin.readlines()
with open(desFileName, "r") as fin:
desLines = fin.readlines()
n = min(len(srcLines), len(desLines))-1
for i in xrange(n):
ans2 = int(desLines[i])
ans1 = int(srcLines[i])
if ans1 > ans2:
print "%d: wrong" % i if __name__ == "__main__":
srcFileName = "F:\Qt_prj\hdoj\data.in"
desFileName = "F:\workspace\cpp_hdoj\data.in"
GenData(srcFileName)
MovData(srcFileName, desFileName)

【HDOJ】1648 Keywords的更多相关文章

  1. 【HDOJ】2222 Keywords Search

    AC自动机基础题. #include <iostream> #include <cstdio> #include <cstring> #include <cs ...

  2. 【HDU】2222 Keywords Search

    [算法]AC自动机 [题解]本题注意题意是多少关键字能匹配而不是能匹配多少次,以及可能有重复单词. 询问时AC自动机与KMP最大的区别是因为建立了trie,所以对于目标串T与自动机串是否匹配只需要直接 ...

  3. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  4. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  5. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

  6. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

  7. 【HDOJ】【2829】Lawrence

    DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...

  8. 【HDOJ】【3415】Max Sum of Max-K-sub-sequence

    DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...

  9. 【HDOJ】【3530】Subsequence

    DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...

随机推荐

  1. MySQL和Navicat使用总结

    1.给字段设置默认字符值 ALTER TABLE `v_users`MODIFY COLUMN `picture` varchar(50) CHARACTER SET utf8 COLLATE utf ...

  2. KnockoutJS(3)-绑定语法

    绑定语法大致分为2种: 1. 数据绑定(data-bind syntax) 2. 绑定上下文(Binding Context) 下面针对这2中绑定语法分别介绍一下 1. 绑定上下文(Binding C ...

  3. laravel扩展xls处理maatwebsite/excel

    github地址:https://github.com/Maatwebsite/Laravel-Excel 安装: sudo composer require maatwebsite/excel 配置 ...

  4. Java语法糖

    1.创建数组:String[] s = new String[]{"1","2","3"};//这种方法可以在不指定数组元素数量的情况下生成 ...

  5. RabbitMQ基础总结

    MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们.消 息传递指的是程序之 ...

  6. Python学习_算数运算函数

    记录以grades列表为例,分别定义输出.求和.平均值.方差和标准差函数,并输出相应的值 grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90 ...

  7. 1072. Gas Station (30)

    先要求出各个加油站 最短的 与任意一房屋之间的 距离D,再在这些加油站中选出最长的D的加油站 ,该加油站 为 最优选项 (坑爹啊!).如果相同D相同 则 选离各个房屋平均距离小的,如果还是 相同,则 ...

  8. Linux 目录操作和4中文件拷贝效率测试

    /*1.用户输入任意目录名称,显示该目录下的文件列表信息,包括文件类型,文件权限,文件大小,文件名称2.拷贝用户输入的文件到当前目录下3.第二点功能,使用4种方式完成,并比较说明效率*/ /* str ...

  9. fedora 解决yumBackend.py进程CPU占用过高

    fedora启动时电脑风扇噪声巨响,检查进行发现是yumBackend.py进行占用CPU过高. yumBackend.py进行是后台检查更新,如果觉得没用可以使用工具关闭检查更新,或者修改检查周期. ...

  10. linux grep、find 命令详解

    grep1.作用grep命令可以指定文件中搜索特定的内容,并将含有这些内容的行标准输出.grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权 ...