链接:https://www.nowcoder.com/acm/contest/148/J
来源:牛客网
时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
Sometimes you may want to write a sentence into your nickname like "lubenwei niubi". But how to change it into a single word? Connect them one by one like "lubenweiniubi" looks stupid.
To generate a better nickname, Rikka designs a non-trivial algorithm to merge a string sequence s1...sn into a single string. The algorithm starts with s=s1 and merges s2...sn into s one by one. The result of merging t into s is the shortest string r which satisfies s is a prefix of r and t is a subsequence of r.(If there are still multiple candidates, take the lexicographic order smallest one.)
String s is a prefix of r if and only if |s| ≤ |r| and for all index i ∈ [1, |s|], si = ri.
String s is a subsequence of r if and only if there is an index sequence which satisfies
.
For example, if we want to generate a nickname from "lubenwei niubi", we will merge "niubi" into "lubenwei", and the result is "lubenweiubi".
Now, given a sentence s1...sn with n words, Rikka wants you to calculate the resulting nickname generated by this algorithm.
输入描述:
The first line contains a single number t(1 ≤ t ≤ 3), the number of testcases.
For each testcase, the first line contains one single integer n(1 ≤ n ≤ 106).
Then n lines follow, each line contains a lowercase string .
输出描述:
For each testcase, output a single line with a single string, the result nickname.
示例1
输入
2
2
lubenwei
niubi
3
aa
ab
abb
输出
lubenweiubi
aabb
以下代码均可AC,啊啊啊,看错了,不是程序跑了7s,是提交用时。。。我就说怎么2秒的限时跑7s还能AC
好烦,调程序调了好久。想麻烦了,但是我的程序也能过,虽然跑了7s
死在了j+c-cnt上,忘记减掉cnt了qwq
耗时268ms
#include<bits/stdc++.h>
#define ll long long
using namespace std;
vector <int> vis[233];
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
string a[n+10];
for(int i=0;i<n;i++)
cin>>a[i];
string s = a[0];
for(int i=0;i<s.size();i++)
vis[s[i]].push_back(i);
for(int i=1;i<n;i++)
{
if(!vis[a[i][0]].size())
{
int c = s.size();
for(int j=0;j<a[i].size();j++)
vis[a[i][j]].push_back(j+c);
s = s+a[i];
continue;
}
int cnt = 1,di=vis[a[i][0]][0];
for(int j=1;j<a[i].size();j++)
{
int pos = upper_bound(vis[a[i][j]].begin(),vis[a[i][j]].end(),di)-vis[a[i][j]].begin();
//printf("%d\n",pos);
if(pos<vis[a[i][j]].size())
{
di = vis[a[i][j]][pos];
//printf("%d\n",di);
cnt++;
}
else
break;
}
//printf("%d\n",cnt);
int c = s.size();
for(int j = cnt ;j<a[i].size();j++)
vis[a[i][j]].push_back(j+c-cnt);
c = a[i].size();
s = s + a[i].substr(cnt,c-cnt);
}
cout<<s<<endl;
for(int i='a';i<='z';i++)vis[i].clear();
}
return 0;
}
然后想着这样写会不会减小复杂度然鹅却是,402 ms
#include<bits/stdc++.h>
#define ll long long
using namespace std;
vector <int> vis[233];
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
string a[n+10];
for(int i=0;i<n;i++)
{
cin>>a[i];
if(i)
{
if(!vis[a[i][0]].size())
{
int c = a[0].size();
for(int j=0;j<a[i].size();j++)
vis[a[i][j]].push_back(j+c);
a[0] = a[0]+a[i];
continue;
}
int cnt = 1,di=vis[a[i][0]][0];
for(int j=1;j<a[i].size();j++)
{
int pos = upper_bound(vis[a[i][j]].begin(),vis[a[i][j]].end(),di)-vis[a[i][j]].begin();
if(pos<vis[a[i][j]].size())
{
di = vis[a[i][j]][pos];
cnt++;
}
else
break;
}
int c = a[0].size();
for(int j = cnt ;j<a[i].size();j++)
vis[a[i][j]].push_back(j+c-cnt);
c = a[i].size();
a[0] = a[0] + a[i].substr(cnt,c-cnt);
}
else
{
for(int j=0;j<a[i].size();j++)
vis[a[i][j]].push_back(j);
}
}
cout<<a[0]<<endl;
for(int i='a';i<='z';i++)vis[i].clear();
}
return 0;
}
然后我觉得c++的string耗时应该比较大,然后改成了下面这个,耗时154 ms
#include<bits/stdc++.h>
using namespace std;
char a[1000005];
char b[1000005];
vector <int> vis[233];
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
scanf("%s",a);
for(int i=0;a[i];i++)
vis[a[i]].push_back(i);
for(int i=0;i<n-1;i++)
{
scanf("%s",b);
if(!vis[b[0]].size())
{
int c = strlen(a);
for(int j=0;b[j];j++)
vis[b[j]].push_back(j+c);
strcat(a,b);
continue;
}
int cnt = 1,di=vis[b[0]][0];
for(int j=1;b[j];j++)
{
int pos = upper_bound(vis[b[j]].begin(),vis[b[j]].end(),di)-vis[b[j]].begin();
if(pos<vis[b[j]].size())
{
di = vis[b[j]][pos];
cnt++;
}
else
break;
}
int c = strlen(a);
for(int j = cnt ;b[j];j++)
vis[b[j]].push_back(j+c-cnt);
strcat(a,b+cnt);
}
printf("%s\n",a);
for(int i='a';i<='z';i++)vis[i].clear();
}
return 0;
}
最后,一种简单的写法(纯暴力,可怕可怕),耗时183ms
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
char a[1000005];
char b[1000005];
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
scanf("%s",a);
n--;
while(n--)
{
scanf("%s",b);
int pos = 0;
int len = strlen(b);
for(int i=0;pos<len && a[i];i++)
if(b[pos]==a[i])
pos++;
strcat(a,b+pos);
}
printf("%s\n",a);
memset(a,0,sizeof(a));
}
return 0;
}