您的位置:首页 > 其它

Codeforces Round #434 (Div. 2) D Polycarp's phone book(字符串,字典树)

2017-09-20 16:48 483 查看
其实是个暴力题..

枚举所有的九位子串,检查它是否会只在某个子串中出现.

具体实现方法是,把所有原串和他们的所有后缀存入字典树,进行一些标记,记录一个边被不同原串经历的次数.

然后就可以枚举遍历.

D题还是不看题解做不出,看了题解也不能瞬间搞懂..

代码强度也很高.

容易re,字典树放大一点.

(不懂啊,为什么70还会RE啊??)

/*  xzppp  */
#include <iostream>
#include <vector>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <list>
#include <string>
#include <cmath>
#include <bitset>
#include <iomanip>
using namespace std;
#define FFF freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define MP make_pair
#define PB push_back
#define _ %MOD
#define MST(x) memset((x),0,sizeof(x))
typedef long long  LL;
typedef unsigned long long ULL;
typedef pair<int,int > pii;
typedef pair<LL,LL> pll;
typedef pair<double,double > pdd;
typedef pair<double,int > pdi;
const int MAXN = 10000+17;
const int MAXM = 1e6+17;
const int MAXV = 2*1e3+17;
const int BIT = 15+3;
const int INF = 0x7f7f7fff;
const LL INFF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9+7;
int pid = 1;
struct dicn
{
int num,son[10],last,tms;
}dic[MAXN*80];
string ans[7*MAXN];
void insert(string str,int id)
{
int now = 0;
for (int i = 0; i < str.length(); ++i)
{
if(dic[now].son[str[i]-'0']==0)
dic[now].son[str[i]-'0'] = pid++;
int nxt = dic[now].son[str[i]-'0'];
if(dic[nxt].last!=id)
{
dic[nxt].tms++;
dic[nxt].last = id;
}
now = nxt;
}
}
void dfs(int rt,string now)
{
for (int i = 0; i <= 9; ++i)
{
int nxt = dic[rt].son[i];
if(nxt!=0)
{
if(dic[nxt].tms==1)
if(ans[dic[nxt].last].length()>now.length()+1||ans[dic[nxt].last].length()==0)
ans[dic[nxt].last] = now+(char)('0'+i);
dfs(nxt,now+(char)(i+'0'));
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
FFF
#endif
int n;
while(cin>>n)
{
for (int i = 0; i < n; ++i)
{
string temp;
cin>>temp;
for (int j = 0; j < temp.size(); ++j)
insert(temp.substr(j),i+1);
}
dfs(0,"");
for (int i = 1; i <= n; ++i)
cout<<ans[i]<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces acm