https://codeforces.com/problemset/gymProblem/102174/B 求条

https://codeforces.com/problemset/gymProblem/102174/B

My.cpp

#include<bits/stdc++.h>
// #define int long long

using namespace std;

// const int Size=(1<<20)+1;
// char buf[Size],*p1=buf,*p2=buf;
// char buffer[Size];
// int op1=-1;
// const int op2=Size-1;
// #define getchar()                                                              \
// (tt == ss && (tt=(ss=In)+fread(In, 1, 1 << 20, stdin), ss == tt)     \
// 	? EOF                                                                 \
// 	: *ss++)
// char In[1<<20],*ss=In,*tt=In;
// inline int read()
// {
// 	int x=0,c=getchar(),f=0;
// 	for(;c>'9'||c<'0';f=c=='-',c=getchar());
// 	for(;c>='0'&&c<='9';c=getchar())
// 		x=(x<<1)+(x<<3)+(c^48);
// 	return f?-x:x;
// }
// inline void write(int x)
// {
// 	if(x<0) x=-x,putchar('-');
// 	if(x>9)  write(x/10);
// 	putchar(x%10+'0');
// }

#ifndef ONLINE_JUDGE
#define ONLINE_JUDGE
#endif

int n,m;
const int N=1e6+5;
int t[N][26];
bool vis[N];
bool vis2[N];
char id[N];
int tot;
int fail[N];

void insert(const string &s)
{
	int nw=0;
	for(char i:s)
	{
		if(!t[nw][i-'a']) t[nw][i-'a']=++tot,id[tot]=i;
		nw=t[nw][i-'a'];
	}
	// cout<<nw<<"\n";
	vis[nw]=1;
}

void build()
{
	memset(vis2,1,sizeof(vis2));
	vis2[0]=0;
	queue<int> q;
	for(int i=0;i<26;i++)
	if(t[0][i]) q.push(t[0][i]);
	
	while(q.size())
	{
		int nw=q.front();
		q.pop();
		for(int i=0;i<26;i++)
		if(t[nw][i])
		{
			fail[t[nw][i]]=t[fail[nw]][i];
			q.push(t[nw][i]);
			vis[t[nw][i]]|=vis[t[fail[nw]][i]];
		}
		else t[nw][i]=t[fail[nw]][i],vis2[nw]=1;
		vis2[nw]=vis[nw];
	}
	for(int i=tot;i>=0;i--) vis2[fail[i]]&=vis2[i];
	// for(int i=0;i<=tot;i++) cout<<vis[i]<<" ";
	// cout<<"\n";
	// for(int i=0;i<=tot;i++) cout<<vis2[i]<<" ";
	// cout<<endl;
}	

void bfs()
{
	int nw=0;
	// cout<<n<<"\n";
	for(int i=0;i<26;i++)
	if(!vis2[t[0][i]])
	{
		putchar(i+'a');
		// cout<<"aaa\n";
		nw=t[0][i];
		break;
	}

	// cout<<nw<<"\n";

	for(int kkk=2;kkk<=n;kkk++)
	{
		for(int i=0;i<26;i++)
		if(!vis2[t[nw][i]])
		{
			nw=t[nw][i];
			putchar(i+'a');
			break;
		}
	}
	putchar('\n');
}

signed main()
{
	// #ifndef ONLINE_JUDGE
	freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);
	// #endif
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
		string s;
		cin>>s;
		insert(s);
	}
	build();
	bfs();
	//mt19937_64 myrand(time(0));
	return 0;
}


/*


小白最近沉迷炼金术。通过炼金术,小白可以获得很多没见过的新物质,例如 $cu+al\to au+cl$。

在这种炼金术中,每一种物质都可以用一个只包含小写字母的字符串来表示。同一个字符串表示相同的物质,不同的字符串表示的物质也不同。如果一种物质对应的字符串包含在另一种物质对应的字符串中,也就是它作为子串出现在另一种物质中,那么这种物质通过炼金术流程可以得到另一种物质。例如,ababc中包含了aba,但abcba中不包含aba。

小白认为一种物质只有它对应的字符串长度恰好为 $n$ 时,它才是最令人愉悦的。现在小白手里已经有 $m$ 种已知物质,他希望找到一种新的物质。该物质不能从已有的 $m$ 种物质中的任何一种中得到,同时也必须是有令人愉悦的特质。聪明的你能帮助他找到一种这样的物质吗?请输出该物质对应的字符串,如果有多种这样的物质存在,你可以输出任意一种,题目保证至少存在一种新的物质。

**Input**

输入共 $m+1$ 行,第一行输入两个正整数 $n\ (1\le n\le 10^5)$ 和 $m\ (1\le m\le 10^4)$ 由空格间隔开,表示新物质对应字符串长度 $n$ 和小白手里已有的物质种类数 $m$。

接下来 $m$ 行,第 $i$ 行输入一个字符串 $s_i$,表示第 $i$ 种物质对应的字符串。字符串只包含小写字母,且保证 $\sum\limits_{i=1}^m |s_i| \le 3\times 10^5$。

**Output**

请输出一个长度为 $n$ 的字符串,描述小白可以炼出的新物质。


*/

Data.cpp:

#include<bits/stdc++.h>
#define int long long

using namespace std;

const int Size=(1<<20)+1;
char buf[Size],*p1=buf,*p2=buf;
char buffer[Size];
int op1=-1;
const int op2=Size-1;
#define getchar()                                                              \
(tt == ss && (tt=(ss=In)+fread(In, 1, 1 << 20, stdin), ss == tt)     \
	? EOF                                                                 \
	: *ss++)
char In[1<<20],*ss=In,*tt=In;
inline int read()
{
	int x=0,c=getchar(),f=0;
	for(;c>'9'||c<'0';f=c=='-',c=getchar());
	for(;c>='0'&&c<='9';c=getchar())
		x=(x<<1)+(x<<3)+(c^48);
	return f?-x:x;
}
inline void write(int x)
{
	if(x<0) x=-x,putchar('-');
	if(x>9)  write(x/10);
	putchar(x%10+'0');
}


signed main()
{
	#ifndef ONLINE_JUDGE
	// freopen("a.in","r",stdin);
	freopen("a.in","w",stdout);
	#endif
	mt19937_64 mr(time(0));
	int n=mr()%10000+1,m=mr()%1000+1;
	cout<<n<<" "<<m<<"\n";
	for(int i=1;i<=m;i++)
	{
		int len=mr()%300+1;
		for(int j=1;j<=len;j++) putchar(mr()%26+'a');
		putchar('\n');
	}
	return 0;
}

/*


小白最近沉迷炼金术。通过炼金术,小白可以获得很多没见过的新物质,例如 $cu+al\to au+cl$。

在这种炼金术中,每一种物质都可以用一个只包含小写字母的字符串来表示。同一个字符串表示相同的物质,不同的字符串表示的物质也不同。如果一种物质对应的字符串包含在另一种物质对应的字符串中,也就是它作为子串出现在另一种物质中,那么这种物质通过炼金术流程可以得到另一种物质。例如,ababc中包含了aba,但abcba中不包含aba。

小白认为一种物质只有它对应的字符串长度恰好为 $n$ 时,它才是最令人愉悦的。现在小白手里已经有 $m$ 种已知物质,他希望找到一种新的物质。该物质不能从已有的 $m$ 种物质中的任何一种中得到,同时也必须是有令人愉悦的特质。聪明的你能帮助他找到一种这样的物质吗?请输出该物质对应的字符串,如果有多种这样的物质存在,你可以输出任意一种,题目保证至少存在一种新的物质。

**Input**

输入共 $m+1$ 行,第一行输入两个正整数 $n\ (1\le n\le 10^5)$ 和 $m\ (1\le m\le 10^4)$ 由空格间隔开,表示新物质对应字符串长度 $n$ 和小白手里已有的物质种类数 $m$。

接下来 $m$ 行,第 $i$ 行输入一个字符串 $s_i$,表示第 $i$ 种物质对应的字符串。字符串只包含小写字母,且保证 $\sum\limits_{i=1}^m |s_i| \le 3\times 10^5$。

**Output**

请输出一个长度为 $n$ 的字符串,描述小白可以炼出的新物质。


*/

std.cpp

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=5e5+10;
struct node{
    int cnt;
    node * nxt[27];
    node * fail;
    vector<node *> num;
}*rt;
node pool[N];
int n,m,idx;
int val[N];
int num;
int f[N];
void insert(string s){
    node *p=rt;
    int i;
    for(i=0;i<s.size();i++){
        int sign=s[i]-'a';
        if(p->nxt[sign]==NULL){
            p->nxt[sign]=pool+(++idx);
            p->nxt[sign]->cnt=++num;
        }
        p=p->nxt[sign];
        if(i==(int)s.size()-1){
            val[p->cnt]=1;
        }
    }
}
void build(){
    int i;
    queue<node *> q;
    rt->fail=rt;
    for(i=0;i<26;i++){
        if(rt->nxt[i]){
            rt->nxt[i]->fail=rt;
            rt->num.push_back(rt->nxt[i]);
            q.push(rt->nxt[i]);
        }
        else{
            rt->nxt[i]=rt;
            rt->nxt[i]->fail=rt;
        }
    }
    while(q.size()){
        auto t=q.front();
        q.pop();
        for(i=0;i<26;i++){
            if(t->nxt[i]){
                t->nxt[i]->fail=t->fail->nxt[i];
                t->fail->nxt[i]->num.push_back(t->nxt[i]);
                q.push(t->nxt[i]);
            }
            else{
                t->nxt[i]=t->fail->nxt[i];
            }
        }
        val[t->cnt]|=val[t->fail->cnt];
    }
}
int st[N];
int dfs(int u,node *p){
    if(f[u]!=-1)
        return f[u];
    f[u]=0;
    st[u]=1;
    for(int i=0;i<26;i++){
        if(!val[p->nxt[i]->cnt]){
            if(!st[p->nxt[i]->cnt])
            f[u]=max(f[u],dfs(p->nxt[i]->cnt,p->nxt[i])+1);
            else
            f[u]=n+1;
        }
    }
    st[u]=0;
    return f[u];
}
int main(){

	freopen("a.in","r",stdin);
	freopen("b.out","w",stdout);
    //ios::sync_with_stdio(false);
    memset(f,-1,sizeof f);
    rt=pool;
    rt->cnt=0;
    int i,j;
    cin>>n>>m;
    for(i=1;i<=m;i++){
        string s;
        cin>>s;
        insert(s);
    }
    build();
    dfs(0,rt);
    int cnt=n;
    node *p=rt;
    for(i=0;i<n;i++){
        cnt--;
        for(j=0;j<26;j++){
            if(f[p->nxt[j]->cnt]>=cnt){
                putchar('a'+j);
                p=p->nxt[j];
                break;
            }
        }
    }
    cout<<endl;
}

fcc.cpp

#include<bits/stdc++.h>
using namespace std;
const long long CHECK_TIMES=1e3; // 对拍次数
const long long TIME_LIMIT=1000; // 时限 (ms)
const long long STACK_MEMORY=256; //栈空间 (MB)
 
string data_outputfile= // 用来存放数据的文件名,对拍结果不同的那组数据
"in";
 
string yours_code_cpp= // 你的代码的文件名
"Gym102174B.cpp";
 
string std_code_cpp= // std 的代码的文件名
"b.cpp";
 
string data_maker_cpp= // 造数据的代码的文件名
"data.cpp";
 
string your_outputfile_out= // 你的代码的输出文件名
"a.out";
 
string std_outputfile_out= // std 的输出文件名
"b.out";
 
vector<string> opt={ // 自定义的编译选项
	"-std=c++14 ", 
	// "-Wall", // 显示最多警告信息
	"-w", // 不显示任何警告
	"-O2", // O2 优化
	"-lm"
};
 
/*
 * you should modify this part to adapt your code
 * ----------------------------------------
*/
 
void check(const string &a);
void surprise();
signed main()
{
	cout<<"Do you use freopen? If the answer is yes, please input 1 , else input 2: "<<endl;
	string a;
	for(int i=1;i<=15;i++)
		cin>>a,check(a);
	surprise();
	return 0;
}
/* ---------------------------------------------------------- */
 
#include<ctime>
#if INT_MAX==RAND_MAX
#include <unistd.h>
#define Sleep(x) usleep(x*1000)
#else
#include <windows.h>
#endif
 
string data_exe="____DATA.exe";
string your_exe="____YOURS.exe";
string std_exe="____STD.exe";
 
void diff_with_files_out();
void diff_with_stdout();
void do_diff_with_stdout_linux(bool f);
void do_diff_with_stdout_windows(bool f);
 
#define del_file(x) " rm "+x+" "
#define make_exe(x,y) " g++ "+x+" "+options+" "+y+" "+Win_sta+" "
#define data_ger(file) (f?"./":"")+file+" > "+data_outputfile+" "
#define code_exe(file,out) (f?"./":"")+file+" < "+data_outputfile+" > "+out
 
void check(const string &a)
{
	cout<<"Checking input... "<<endl;
	if(a.size()==1)
	{
		if(a=="1") diff_with_files_out(),exit(0);
		else if(a=="2") diff_with_stdout(),exit(0);
		else cout<<"The input is illegal! You have no egg!!!\n";
	}
	else cout<<"The input is illegal! You have no egg!!!\n";
}
 
void diff_with_files_out()
{	
	cout<<"The input is legal!"<<endl;
	cout<<"=============================================== "<<endl;
	Sleep(300);
	cout<<"initializing..."<<endl;
 
	string Win_sta=" ";
	int awa=1;
	bool f=RAND_MAX==INT_MAX;
	if(f) awa=system(("ulimit -s "+to_string(STACK_MEMORY*1024)).c_str());
	else Win_sta+="-Wl,--stack="+(to_string(STACK_MEMORY*1024*1024));
	assert(awa+1);
 
	string your_code_operation="g++ "+yours_code_cpp;
	string std_code_operation="g++ "+std_code_cpp;
	string data_maker_operation="g++ "+data_maker_cpp;
	for(const string &op:opt)
	{
		your_code_operation+=" "+op;
		std_code_operation+=" "+op;
		data_maker_operation+=" "+op;
	}
	your_code_operation+=" -o "+your_exe+" "+Win_sta;
	std_code_operation+=" -o "+std_exe+" "+Win_sta;
	data_maker_operation+=" -o "+data_exe+" "+Win_sta;
 
	string comparsion_operation;
	if(f) comparsion_operation="diff -Z "+your_outputfile_out+" "+std_outputfile_out+" ";
	else comparsion_operation="fc "+your_outputfile_out+" "+std_outputfile_out+" ";
 
	if(f)
	{
		data_exe="./"+data_exe;
		your_exe="./"+your_exe;
		std_exe="./"+std_exe;
	}
	data_exe+=" > "+data_outputfile+" ";
	your_exe+=" < "+data_outputfile+" > "+your_outputfile_out+" ";
	std_exe+=" < "+data_outputfile+" > "+std_outputfile_out+" ";
 
	awa=system(your_code_operation.c_str());
	awa=system(std_code_operation.c_str());
	awa=system(data_maker_operation.c_str());
 
	cout<<"initializing Succeeded!!!"<<endl;
	cout<<"=============================================== "<<endl;
	Sleep(200);
	cout<<"Running..."<<endl;
	Sleep(200);
 
	for(signed i=1;i<=CHECK_TIMES;i++)
	{	
		printf("Running on test #%d,",i);
		if(i<10)      printf("    ");
		else if(i<100) printf("   ");
		else if(i<1000) printf("  ");
		else if(i<10000) printf(" ");
		// fflush(stdout);
 
		awa=system(data_exe.c_str());
 
		std::chrono::milliseconds mbs = std::chrono::duration_cast< std::chrono::milliseconds >(std::chrono::system_clock::now().time_since_epoch());
		long long bgt=mbs.count();
		awa=system(your_exe.c_str());
		std::chrono::milliseconds mes = std::chrono::duration_cast< std::chrono::milliseconds >(std::chrono::system_clock::now().time_since_epoch());
		long long edt=mes.count();
		long long dt=edt-bgt;
		printf("time=%lldms",dt);
		printf(", ");
		if(dt<10)      printf("   ");
		else if(dt<100) printf("  ");
		else if(dt<1000) printf(" ");
 
		if(!f) putchar('\n');
		// fflush(stdout);
 
		awa=system(std_exe.c_str());
		bool flag=0;
		if(dt>TIME_LIMIT) assert(!system("echo 'Time Limit Error !!!'")),flag=1;
 
		if(system(comparsion_operation.c_str())) { printf("Wrong Answer! \n"); flag=1; }
		else if(f) printf("Accepted");
 
		if(flag) exit(0);
		if(f)cout<<'\n';
		else puts("Accepted\n-----------------------------------------------\n");
		fflush(stdout);
	}
}
 
void surprise()
{
	for(int i=1;i<=10000;i++)
	cout<<"You have no egg!!!\n",Sleep(50);
}
 
void diff_with_stdout()
{
	cout<<"The input is legal!"<<endl;
	cout<<"=============================================== "<<endl;
	Sleep(300);
	cout<<"initializing..."<<endl;
 
	bool f=RAND_MAX==INT_MAX;
 
	if(f) do_diff_with_stdout_linux(f);
	else do_diff_with_stdout_windows(f);
}
 
void do_diff_with_stdout_linux(bool f)
{
	string Win_sta=" ";
	int awa=1;
	if(f) awa=system(("ulimit -s "+to_string(STACK_MEMORY*1024)).c_str());
	else Win_sta+="-Wl,--stack="+(to_string(STACK_MEMORY*1024*1024));
	assert(awa+1);
	int cnt=0;
 
	string your_code_operation="g++ "+yours_code_cpp;
	string std_code_operation="g++ "+std_code_cpp;
	string data_maker_operation="g++ "+data_maker_cpp;
	for(const string &op:opt)
	{
		your_code_operation+=" "+op;
		std_code_operation+=" "+op;
		data_maker_operation+=" "+op;
	}
	your_code_operation+=" -o "+your_exe+" "+Win_sta;
	std_code_operation+=" -o "+std_exe+" "+Win_sta;
	data_maker_operation+=" -o "+data_exe+" "+Win_sta;
 
	string e_rand,e_a,e_b;
	e_rand=data_ger(data_exe);
	e_a=code_exe(your_exe,your_outputfile_out);
	e_b=code_exe(std_exe,std_outputfile_out);
 
	string comparsion_operation;
	if(f) comparsion_operation="diff -Z "+your_outputfile_out+" "+std_outputfile_out+" ";
	else comparsion_operation="fc "+your_outputfile_out+" "+std_outputfile_out+" ";
 
	awa=system(your_code_operation.c_str());
	awa=system(std_code_operation.c_str());
	awa=system(data_maker_operation.c_str());
 
	cout<<"initializing Succeeded!!!"<<endl;
	cout<<"=============================================== "<<endl;
	Sleep(200);
	cout<<"Running..."<<endl;
	Sleep(200);
	for(int kkk=1;kkk<=CHECK_TIMES;kkk++)
	{
		printf("#%d------->", ++cnt);
		assert(!system(e_rand.c_str()));
 
		std::chrono::milliseconds mbs = std::chrono::duration_cast< std::chrono::milliseconds >(std::chrono::system_clock::now().time_since_epoch());
		long long bgt=mbs.count();
		assert(!system(e_a.c_str()));
		std::chrono::milliseconds mes = std::chrono::duration_cast< std::chrono::milliseconds >(std::chrono::system_clock::now().time_since_epoch());
		long long edt=mes.count();
		printf("time: %lld  ",edt-bgt);
		if(edt-bgt>TIME_LIMIT) assert(!system("echo Time Limit Error")), exit(0);
		
		assert(!system(e_b.c_str()));
 
		if(system(comparsion_operation.c_str())) assert(!system("echo Wrong Answer")), exit(0);
		puts("Accepted");
	}
}
 
void do_diff_with_stdout_windows(bool f)
{
	string Win_sta=" ";
	int awa=1;
	if(f) awa=system(("ulimit -s "+to_string(STACK_MEMORY*1024)).c_str());
	else Win_sta+="-Wl,--stack="+(to_string(STACK_MEMORY*1024*1024));
	assert(awa+1);
	int cnt=0;
 
	string your_code_operation="g++ "+yours_code_cpp;
	string std_code_operation="g++ "+std_code_cpp;
	string data_maker_operation="g++ "+data_maker_cpp;
	for(const string &op:opt)
	{
		your_code_operation+=" "+op;
		std_code_operation+=" "+op;
		data_maker_operation+=" "+op;
	}
	your_code_operation+=" -o "+your_exe+" "+Win_sta;
	std_code_operation+=" -o "+std_exe+" "+Win_sta;
	data_maker_operation+=" -o "+data_exe+" "+Win_sta;
 
	string comparsion_operation;
	if(f) comparsion_operation="diff -Z "+your_outputfile_out+" "+std_outputfile_out+" ";
	else comparsion_operation="fc "+your_outputfile_out+" "+std_outputfile_out+" ";
 
	awa=system(your_code_operation.c_str());
	awa=system(std_code_operation.c_str());
	awa=system(data_maker_operation.c_str());
 
	string e_rand,e_a,e_b;
	e_rand=data_ger(data_exe);
	e_a=code_exe(your_exe,your_outputfile_out);
	e_b=code_exe(std_exe,std_outputfile_out);
 
	cout<<"initializing Succeeded!!!"<<endl;
	cout<<"=============================================== "<<endl;
	Sleep(200);
	cout<<"Running..."<<endl;
	Sleep(200);
	// return;
	for(int kkk=1;kkk<=CHECK_TIMES;kkk++)
	{
		printf("#%d------->", ++cnt);
		assert(!system(e_rand.c_str()));
 
		std::chrono::milliseconds mbs = std::chrono::duration_cast< std::chrono::milliseconds >(std::chrono::system_clock::now().time_since_epoch());
		long long bgt=mbs.count();
		assert(!system(e_a.c_str()));
		std::chrono::milliseconds mes = std::chrono::duration_cast< std::chrono::milliseconds >(std::chrono::system_clock::now().time_since_epoch());
		long long edt=mes.count();
		printf("time: %lld  \n",edt-bgt);
		if(edt-bgt>TIME_LIMIT) assert(!system("echo Time Limit Error")), exit(0);
 
		assert(!system(e_b.c_str()));
 
		if(system(comparsion_operation.c_str())) assert(!system("echo Wrong Answer")), exit(0);
		puts("Accepted\n-----------------------------------------------\n");
	}
}
posted @ 2025-11-10 20:41  Wy_x  阅读(24)  评论(0)    收藏  举报