Finding string
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 4 Accepted Submission(s): 2
Problem Description Richard is a smart data analyst in a famous company. One of his daily boring work is to find how many times does a pattern string occur in a very long string.
Luckily, Richard notices that there are many consecutive repeated substrings in this very long string, so he uses the following compression algorithm to make the string shorter:
a). Find a non-compressed consecutive repeated substring of the original string, e.g. “ab” in “cabababd”.
b). Replace the repeating part with the bracketed repetend, followed by the times the repetend appears in the original string. e.g. Write “cabababd” as “c[ab]3d”. Note she can also write it as “c[ab]1ababd” or “ca[ba]2bd” and so on, although these string are not compressed as well as the first one is.
c). Repeat a) and b) several times until the string is short enough.
However, Richard finds it still a bit hard for him to do his work after the compression. So he orders you to write a program to make his work easier. Can you help him?
Input The input contains several test cases, terminated by EOF. The number of test cases does not exceed 10000.
Each test case contains two lines, denote the compressed text and pattern string. The decompressed text and pattern string contain lowercase letter only. The first line contains only lowercase letters (a-z), square brackets ([]) and numbers (0-9), and the second line contains only lowercase letters (a-z). The brackets must be followed with an integer t(1≤t≤2 31-1)indicating the times the string in the brackets repeat. The brackets won't be nested.
You can assume the length of the compressed string and pattern string won't exceed 500.
Please note that for most test cases, the length of the pattern string is relatively very small.
Output For each test case, print a single line containing the times this pattern string occurs in the very long string.
Sample Input
[ab]10aaba
ba
Sample Output
11
Source 2013 Multi-University Training Contest 7
Recommend zhuyuanchen520
题目大意:
就是问模式串在一个长串中出现了多少次,但是这个长串的表示方式是可以这样的[ab]10表示连续10个ab,样例中的[ab]10aaba表示的串就是: ababababababababababaaba
解题思路:
我的方法是,KMP,在带有循环的串中匹配的时候记录路径,直到遇到循环,然后处理一下。题目case数比较多,不能每次都memset的。。。
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<map>
using namespace std;
const int N = 510;
char s[N],str[N];
struct Node{
string substr;
long long count;
}node[N];
int sum;
int next[N];
int path[N*N];
int match[N*N];
map<int,int>vis;
void get_next(){
next[0]=0;
for(int i=1;str[i];i++){
int j=next[i-1];
while(j&&str[i]!=str[j])j=next[j-1];
if(str[i]==str[j])j++;
next[i]=j;
}
}
int tmp[N];
int get_sub_len(string ss){
tmp[0]=0;
for(int i=1;i<ss.length();i++){
int j=tmp[i-1];
while(j&&ss[i]!=ss[j])j=tmp[j-1];
if(ss[i]==ss[j])j++;
tmp[i]=j;
}
return tmp[ss.length()-1];
}
void analyse(){
sum=0;
node[sum].substr.clear();
node[sum].count=0;
for(int i=0;s[i];i++){
if(s[i]=='['){
node[sum].substr.clear();
node[sum].count=0;
i++;
while(s[i]!=']')node[sum].substr+=s[i++];
i++;
node[sum].count=0;
while(isdigit(s[i])){
node[sum].count=node[sum].count*10+s[i]-'0';
i++;
}
i--;
int t=get_sub_len(node[sum].substr);
if(node[sum].substr.length()%(node[sum].substr.length()-t)==0){
node[sum].count=node[sum].count*node[sum].substr.length()/(node[sum].substr.length()-t);
node[sum].substr=node[sum].substr.erase(node[sum].substr.length()-t);
}
sum++;
}else{
node[sum].substr.clear();
node[sum].count=1;
node[sum].substr+=s[i];
sum++;
}
}
}
long long get_ans(){
int now=0;
int n=strlen(str);
long long ans=0;
for(int i=0;i<sum;i++){
vis.clear();
match[0]=0;
int len=node[i].substr.length();
long long count=node[i].count;
long long last=len*count;
for(int j=1;j<=last;j++){
int k=(j-1)%len;
while(now&&node[i].substr[k]!=str[now])now=next[now-1];
if(node[i].substr[k]==str[now])now++;
match[j]=match[j-1];
if(now==n){
match[j]++;
ans++;
now=next[now-1];
}
path[j]=now;
if(vis.count(now*N+k)!=0){
int t=vis[now*N+k];
ans=ans+(last-j)/(j-t)*(match[j]-match[t]);
ans+=(match[t+(last-j)%(j-t)]-match[t]);
now=path[t+(last-j)%(j-t)];
break;
}else{
vis[now*N+k]=j;
}
}
}
return ans;
}
int main(){
while(~scanf("%s%s",s,str)){
analyse();
/*
for(int i=0;i<sum;i++){
printf("%s %d\n",node[i].substr.c_str(),node[i].count);
}
*/
get_next();
printf("%I64d\n",get_ans());
}
}