随笔分类 -  字符串

摘要:题目:给出n个串,问最多能够选出多少个串,使得前面串是后面串的子串(按照输入顺序)分析: 其实这题是这题SPOJ 7758. Growing Strings AC自动机DP的进阶版本,主题思想差不多。 对于这题来说,需要离线操作。dp转移也是很显然。 但是由于数据比较大,所以普通的沿着fail指针往上走,逐步更新答案会TLE。 考虑把fail指针反向,由于ac自动机的每个节点均有唯一的fail指针,若是沿着fail指针往上走,显然都会走到root,所以反向之后显然是一棵树,不妨称之为fail树。fail树有什么特点呢?可以画个图试试,如果儿子节点出现过,那么他的祖先显然也会出现! 因... 阅读全文
posted @ 2013-10-06 11:45 yejinru 阅读(433) 评论(11) 推荐(0)
摘要:Growing Strings题目:给出n个字符串,问最多能够选出多少个串组成序列,并满足前一个字符串是后一个字符串的子串。分析: AC自动机经典水题。。。 考虑每个节点结尾时,他能够选出最多的串 = max{ 父节点选出 , fail节点选出}+以该节点为结尾的单词个数#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std;typedef long long ll;type.. 阅读全文
posted @ 2013-10-05 20:38 yejinru 阅读(393) 评论(0) 推荐(0)
摘要:E. e-Government题目: 给出n个字符串,表示n个人名,有两种操作: ?string ,统计字符串string中出现的属于城市居民的次数。 +id,把编号为id的人变为城市居民,如果已经是忽略。 -id,把编号为id的人变为不是城市居民,如果已经不是的话忽略。 现有m个操作,对于?输出结果。分析: 很容易想到建立ac自动机,+-操作倒简单,使用bool数组标记一下即可。对于每个询问,每次都沿着fail指针往上走,遇到了标记为城市居民的字符串时加1,否则继续往上走。 在这显然会TLE。很容易构造出以下数据: a aa aaa ... aaa...aaa ?a... 阅读全文
posted @ 2013-09-16 09:45 yejinru 阅读(477) 评论(0) 推荐(0)
摘要:题目: 问给出的数字在字符串中出现的次数(有映射关系的) 分析: 直接map<int,int> 记录给出的数,然后对于每个字符串,可以先转换成一个整数(长度小于7),然后若该数在map中, 这更新相应的计数器即可若代码有错,请指出^^67268252012-09-09 17:10:30Accepted428793MS688K839 BG++View Code 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <map> 5 6 using 阅读全文
posted @ 2012-09-09 17:24 yejinru 阅读(896) 评论(1) 推荐(0)
摘要:poj 1226 Substringshttp://poj.org/problem?id=1226/*题目: 查找最长子串在所有给出的字符串或者它的反转的串中出现分析: 二分枚举所有可能的长度,然后用KMP算法判断该枚举的子串是否在所有给出的字符串 或者它的翻转串中出现。*/#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int X = 205;char in[X][X],p[X];int fail[X],len[X],n,lens;int mid 阅读全文
posted @ 2012-05-30 19:43 yejinru 阅读(265) 评论(0) 推荐(0)
摘要:/*trie树建立,然后递归打印所有的电话号码(统计次数大于1的打印出来),内存在HOJ上爆了,应该是递归时爆栈了。。。第二个代码两个都是可以AC的,要注意POJ上是只输入一个案例,HOJ上输入多个样例39268K938MS第二个14232K891MS*/#include <cstdio>#include <cstring>#include <iostream>using namespace std;#define X 125char s[X];int n;struct trie{ int id; trie *p[10]; char s[X]; trie() 阅读全文
posted @ 2012-05-07 10:34 yejinru 阅读(221) 评论(0) 推荐(1)
摘要:/*题目1)简介输入:正则表达式字符串 待匹配字符串,如果输入”### ###”程序终止,输出:如匹配成功,输出整个字符串,如果匹配不成功,输出lost,然后等待下一次用户的输入。^ 代表字符串开始. 代表任意字符$ 代表字符串末尾2)参考输入,输出^a.$ abc ←regular express and target string (separate by space),inputlost ←result, outputab.$ ababc ←regular express and target string (separate by space), inputababc ←match s 阅读全文
posted @ 2012-02-29 22:41 yejinru 阅读(5925) 评论(0) 推荐(1)
摘要:编辑器加载中...本电子词典功能:查单词,单词复习,单词学习,单词填空,根据汉语输入英文,有时间提示的单词风暴第一部分:main函数编写#include <stdio.h>#include <string.h>#include "list.h"#include "tool.h"#include "game.h"#include <stdlib.h>int main(){int m=0,n=7950,c;char ch[130];struct wordnode *head=NULL;head=Crea 阅读全文
posted @ 2012-02-29 22:40 yejinru 阅读(694) 评论(0) 推荐(1)
摘要:#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define X 1000005char s[X],p[X];int Next[X],len1,len2;void shift() //计算移动的模板{int i,j = -1;Next[0] = -1;for(i=1;i<len2;i++){while(j!=-1&&s[j+1]!=s[i])j = Next[j];if(s[j+1]==s[i])j++;Next[i] = j;}}in 阅读全文
posted @ 2012-02-29 22:12 yejinru 阅读(165) 评论(0) 推荐(0)
摘要:题目: 输入一串数字,求用这些数字能组成最大的整数分析: 可以用sort()函数比较排序,比较方式为 int cmp(string a,string b){return a+b>b+a;}即可#include <iostream>#include <algorithm>#include <string>using namespace std;string s[52];int cmp(string a,string b){ return a+b>b+a;}int main(){ freopen("sum.in","r& 阅读全文
posted @ 2012-02-22 19:09 yejinru 阅读(139) 评论(0) 推荐(0)