Submission #850053

# Submission time Handle Problem Language Result Execution time Memory
850053 2023-09-15T16:56:11 Z dshfjka Type Printer (IOI08_printer) C++14
90 / 100
1000 ms 57272 KB
#include <bits/stdc++.h>
#define LL long long
using namespace std;
vector<char>v;
int dp[500001];
// kata terpanjang dimaksukin nanti aja bos
struct Trie{
	int masuk=0;
	struct node{
		int cnt;
		int child[26];
	};
	vector<node>t;
	Trie(){
		t.emplace_back();
	}
	void insert(int depth ,int pos, string s)
	{
		if(depth==s.length())
		{
			t[pos].cnt++;
			return;
		}
		if(!t[pos].child[s[depth]-'a']){
			t[pos].child[s[depth]-'a']=t.size();
			t.emplace_back();
		}
		insert(depth+1,t[pos].child[s[depth]-'a'],s);
		dp[pos]=max(dp[pos],dp[t[pos].child[s[depth]-'a']]+1);
	//	t[pos].cnt++;
	}
	void query( int pos, bool status)
	{
//		if(++masuk ==60)exit(0);
	//	cout<<"pos :"<<pos<<"  status :"<<status<<endl;
		bool masuk=0;
		int maks=-1,ket=-1;
		for(int i=0;i<t[pos].cnt;i++)v.push_back('P');
		if(status==1)
		{
			for(int a=0;a<26;a++)
			{
				if(t[pos].child[a])
				{
					if(dp[t[pos].child[a]]>maks)
					{
						maks=dp[t[pos].child[a]];
						ket=a;
					}
				}
			}
		}
	//	printf("maks=%d dan ket=%c\n",maks,ket+'a');
		for(int a=0;a<26;a++)
		{
			
			if(t[pos].child[a] && ket!=a)
			{
				masuk=1;
			//	printf("MASUK dengan %c \n",a+'a');
				masuk=1;
				v.push_back(a+'a');
				query(t[pos].child[a],0);
				v.push_back('-');
			}
			
		}
		if(status && ket!=-1)
		{
			masuk=1;
			v.push_back(ket+'a');
			query(t[pos].child[ket],1);
		}
		
	}
};
int main()
{
	int n;
	scanf("%d",&n);
	Trie trie;
	for(int a=1;a<=n;a++)
	{
		string x;
		cin>>x;
		trie.insert(0,0,x);
	}
	trie.query(0,1);
	printf("%d\n",v.size());
	for(char a:v)cout<<a<<endl;
}

Compilation message

printer.cpp: In member function 'void Trie::insert(int, int, std::string)':
printer.cpp:19:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   19 |   if(depth==s.length())
      |      ~~~~~^~~~~~~~~~~~
printer.cpp: In member function 'void Trie::query(int, bool)':
printer.cpp:36:8: warning: variable 'masuk' set but not used [-Wunused-but-set-variable]
   36 |   bool masuk=0;
      |        ^~~~~
printer.cpp: In function 'int main()':
printer.cpp:89:11: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wformat=]
   89 |  printf("%d\n",v.size());
      |          ~^    ~~~~~~~~
      |           |          |
      |           int        std::vector<char>::size_type {aka long unsigned int}
      |          %ld
printer.cpp:80:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |  scanf("%d",&n);
      |  ~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 604 KB Output is correct
2 Correct 9 ms 808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 1336 KB Output is correct
2 Correct 21 ms 2164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 62 ms 5136 KB Output is correct
2 Correct 159 ms 8016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 159 ms 8016 KB Output is correct
2 Correct 49 ms 2168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 392 ms 29896 KB Output is correct
2 Correct 885 ms 57272 KB Output is correct
3 Correct 489 ms 29624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 319 ms 16092 KB Output is correct
2 Execution timed out 1034 ms 57108 KB Time limit exceeded
3 Halted 0 ms 0 KB -