赞
踩
题意:
每个字符有个价值 一个字符串的价值是所有字符和 输入一些字符和一个字典 每种方法之多包含字典里两个串 每种方法的字符必须都包含在输入的一堆字符中 问 方法最大价值是多大 按字典序输出所有方法 (每个串最多7个字符)
思路:
字典中4W个单词 要是n^2枚举所有方法一定会TLE 但是串最多7个字符值得利用
输入的一堆字符一用7!种排列 每种方法最多两个串 也就是说可以枚举7*7即可得到所有方法 然后看方法中的串是否都在字典中 在根据题意维护价值最大
代码:
- /*
- ID: housera1
- PROG: lgame
- LANG: C++
- */
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #include<string>
- #include<set>
- #include<iostream>
- using namespace std;
-
- int val[]={2,5,4,4,1,6,5,5,1,7,6,3,5,2,3,5,7,2,1,2,4,6,6,7,5,7};
- set<string> st;
- string fir,str,tmp1,tmp2;
- int ans;
- struct ways
- {
- string a,b;
- bool operator<(const ways fa) const
- {
- if(a!=fa.a) return a<fa.a;
- return b<fa.b;
- }
- }way;
- set<ways> fzc;
- set<ways>::iterator it;
-
- int main()
- {
- ios::sync_with_stdio(false);
- int Debug=0;
- if(!Debug)
- {
- freopen("lgame.in","r",stdin);
- freopen("lgame.out","w",stdout);
- }
- int i,j,k,len,sz,num1,num2,sum;
- cin>>fir;
- len=fir.size();
- sort(fir.begin(),fir.end());
- if(!Debug) freopen("lgame.dict","r",stdin);
- while(cin>>str)
- {
- if(str==".") break;
- st.insert(str);
- }
- do
- {
- for(i=0;i<len;i++)
- {
- tmp1=fir.substr(0,i+1);
- if(st.find(tmp1)==st.end()) continue;
- for(k=0,sz=tmp1.size(),num1=0;k<sz;k++) num1+=val[tmp1[k]-'a'];
- for(j=i;j<len;j++)
- {
- num2=0;
- if(i!=j)
- {
- tmp2=fir.substr(i+1,j-i);
- if(tmp2<tmp1||st.find(tmp2)==st.end()) continue;
- for(k=0,sz=tmp2.size();k<sz;k++) num2+=val[tmp2[k]-'a'];
- }
- else tmp2.clear();
- sum=num1+num2;
- if(sum>ans)
- {
- ans=sum;
- fzc.clear();
- way.a=tmp1;
- way.b=tmp2;
- fzc.insert(way);
- }
- else if(sum==ans)
- {
- way.a=tmp1;
- way.b=tmp2;
- fzc.insert(way);
- }
- }
- }
- }while(next_permutation(fir.begin(),fir.end()));
- cout<<ans<<endl;
- for(it=fzc.begin();it!=fzc.end();it++)
- {
- cout<<(*it).a;
- if(!(*it).b.empty()) cout<<" "<<(*it).b;
- cout<<endl;
- }
- return 0;
- }
- /*
- prmgroa
- profile
- program
- prom
- rag
- ram
- rom
- .
- */

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。