Finding string
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 4 Accepted Submission(s): 2
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?
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≤231-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.
題目大意:
解題思路:
代碼:
#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());
}
}