URAL 1002 Phone Numbers(KMP+最短路orDP)
1 ij 2 abc 3 def |
Input
Output
No solution.”. If there are more solutions having the minimum number of words, you can choose any single one of them.#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std; const int MAXV = ;
const int MAXS = ;
const int MAXL = ;
const int MAXE = MAXS * MAXL; int number[] = {, , , , , , , , , , , , , , , , , , , , , , , , , };
int mat[MAXV][MAXV], pre[MAXV], ecnt, n, k;
int s[MAXS][MAXL], src[MAXV], len[MAXS];
char ans[MAXS][MAXL], str[MAXV]; void init() {
memset(mat, -, sizeof(mat));
} void add_edge(int u, int v, int pos) {
mat[u][v] = pos;
} int dis[MAXV]; bool SPFA() {
memset(dis, 0x3f, sizeof(dis));
dis[] = ;
for(int i = ; i < n; ++i) {
for(int j = i + ; j <= n; ++j) {
if(~mat[i][j] && dis[i] + < dis[j]) {
pre[j] = mat[i][j];
dis[j] = dis[i] + ;
}
}
}
return dis[n] <= n;
} void getFail(int P[], int m, int f[]) {
f[] = f[] = ;
for(int i = ; i < m; ++i) {
int j = f[i];
while(j && P[i] != P[j]) j = f[j];
f[i + ] = (P[i] == P[j] ? j + : );
}
} void KMP(int T[], int n, int P[], int m, int f[], int pos) {
getFail(P, m, f);
for(int i = , j = ; i < n; ++i) {
while(j && P[j] != T[i]) j = f[j];
if(P[j] == T[i]) ++j;
if(j == m) add_edge(i - m + , i + , pos);
}
} void print(int pos) {
if(pos - len[pre[pos]] != ) {
print(pos - len[pre[pos]]);
putchar(' ');
}
printf("%s", ans[pre[pos]]);
} int fail[MAXL]; int main() {
while(scanf("%s", str) != EOF) {
if(strcmp(str, "-1") == ) break;
n = strlen(str);
for(int i = ; i < n; ++i) src[i] = str[i] - '';
scanf("%d", &k);
for(int i = ; i < k; ++i) {
scanf("%s", ans[i]);
len[i] = strlen(ans[i]);
for(int j = ; j < len[i]; ++j) s[i][j] = number[ans[i][j] - 'a'];
}
init();
for(int i = ; i < k; ++i)
KMP(src, n, s[i], len[i], fail, i);
if(SPFA()) print(n), puts("");
else puts("No solution.");
}
}
URAL 1002 Phone Numbers(KMP+最短路orDP)的更多相关文章
- 1002 Phone Numbers 解题报告
1002. Phone Numbers Time limit: 2.0 secondMemory limit: 64 MB In the present world you frequently me ...
- 递推DP URAL 1586 Threeprime Numbers
题目传送门 /* 题意:n位数字,任意连续的三位数字组成的数字是素数,这样的n位数有多少个 最优子结构:考虑3位数的数字,可以枚举出来,第4位是和第3位,第2位组成的数字判断是否是素数 所以,dp[i ...
- 递推DP URAL 1009 K-based Numbers
题目传送门 题意:n位数,k进制,求个数分析:dp[i][j] 表示i位数,当前数字为j的个数:若j==0,不加dp[i-1][0]; 代码1: #include <cstdio> #in ...
- ural 2070. Interesting Numbers
2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...
- ural 1150. Page Numbers
1150. Page Numbers Time limit: 1.0 secondMemory limit: 64 MB John Smith has decided to number the pa ...
- URAL 1056 Computer Net(最短路)
Computer Net Time limit: 2.0 secondMemory limit: 64 MB Background Computer net is created by consecu ...
- URAL 2034 Caravans(变态最短路)
Caravans Time limit: 1.0 secondMemory limit: 64 MB Student Ilya often skips his classes at the unive ...
- URAL 2031. Overturned Numbers (枚举)
2031. Overturned Numbers Time limit: 1.0 second Memory limit: 64 MB Little Pierre was surfing the In ...
- URAL 1012 K-based Numbers. Version 2(DP+高精度)
题目链接 题意 :与1009一样,不过这个题的数据范围变大. 思路:因为数据范围变大,所以要用大数模拟,用java也行,大数模拟也没什么不过变成二维再做就行了呗.当然也可以先把所有的都进行打表,不过要 ...
随机推荐
- NSNotificationCenter带参
(1)post Notification类 [[NSNotificationCenter defaultCenter] postNotificationName:CRMPerformanceNewCe ...
- javascript 模式方面的学习
看了好多网上的文章,基本上得到一个结论:一些写类工具函数或框架的写类方式本质上都是 构造函数+原型 1.用构造函数来定义类属性(字段).2.用原型方式来定义类的方法. 具体文章请参阅 JavaScri ...
- [LeetCode]题解(python):074-Search a 2D Matrix
题目来源 https://leetcode.com/problems/search-a-2d-matrix/ Write an efficient algorithm that searches fo ...
- [LeetCode]题解(python):049-Groups Anagrams
题目来源 https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For ...
- [LeetCode]题解(python):050-Pow(x, n)
题目来源 https://leetcode.com/problems/powx-n/ Implement pow(x, n). 题意分析 Input: x,n Output:pow(x,n) Cond ...
- EChars学习-1
Echarts,编写来自Enterprise Charts,商业级数据图表,是百度的一个开源的数据可视化工具 官网地址:http://echarts.baidu.com/ 一.引入Echarts &l ...
- JQuery事件的链式写法
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- Java学习-037-JavaWeb_006 -- JSP 动作标识 - include
这个动作是指在当前的页面中包含一个或多个 JSP 页面或者 HTML 文件,语法:<jsp:include file="../jsp/login.jsp" flush=&qu ...
- c#基础,面试前迅速巩固c#最基础知识点
n年前为了面试,搜罗的C#基础知识,记在了文档里.今天写到博客园里,与人分享,因为不是专家,所以仅供参考. 1.面向对象 在面向对象概念提出之前,语言都是面向过程的,说到面向对象,应该与面向过程比较, ...
- apache httpclient 4.5 兼容 http https
String responseContent = ""; try { SSLContextBuilder contextBuilder = new SSLContextBuilde ...