当前位置:网站首页>Yugu p1012 spelling +p1019 word Solitaire (string)
Yugu p1012 spelling +p1019 word Solitaire (string)
2022-07-06 13:51:00 【zjsru_ Beginner】
Topic 1 ( Spell numbers )
Equipped with nn A positive integer a_1 \dots a_na1…an, Join them in a row , Adjacent numbers end to end , Make up the largest integer .
Input format
The first line has an integer , Represents the number of numbers nn.
The second line has nn It's an integer , Indicates the given nn It's an integer a_iai.
Output format
A positive integer , Represents the largest integer
I/o sample
Input
3 13 312 343
Output
34331213
Input
4 7 13 4 246
Output
7424613
Their thinking
The question is relatively simple , It is a problem of string splicing to get the largest integer . Change the code bool cmp Judgment is important , The judgment is a+b and b+a String size , instead of a and b The size of the string , Because judging alone a and b The number spliced by the size of may not be the maximum ( for example 521 and 52), And then main In the function sort Sort , Output results .
The code is as follows
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int n;
string s[20];
bool cmp(string a, string b) {
return a + b > b + a;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> s[i];
}
sort(s+1, s + n+1 , cmp);
for (int i = 1; i <= n; i++){
cout << s[i];
}
return 0;
}
The operation results are as follows :
Topic two ( The word chain )
The word Solitaire is a game similar to the idiom Solitaire we often play , Now we know a set of words , And given a starting letter , Ask for the longest beginning with this letter “ dragon ”( Every word is at most “ dragon ” There are two times ), When two words are connected , The overlapping parts are combined into one part , for example beast
and astonish
, If connected into a dragon, it becomes beastonish
, In addition, two adjacent parts cannot have a containment relationship , for example at
and atide
Can't be connected .
Input format
The first line of input is a single integer nn Indicates the number of words , following nn Each line has a word , The last line of input is a single character , Express “ dragon ” The first letter . You can assume that “ dragon ” There must be .
Output format
Just output the longest... Starting with this letter “ dragon ” The length of .
I/o sample
Input
5 at touch cheat choose tact a
Output
23
Their thinking
This question should pay attention to three points :1. Use each word twice at most
2. Words can be left unfinished , The longer the length, the better
3. The less overlap, the better , The longer the Dragon
There are two functions in the following code , among xianjie Function the connection of two strings , from 1 Start , Connect the two shortest strings , Returns the length of the smallest overlap , stay solve Search in function , Get the maximum string length .
The code is as follows
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int a[20];
int n,length=0;
string s[20];
int xianjie(string s1,string s2){
for(int i=1;i<min(s1.length(),s2.length());i++){ // Length of overlap
int flag=1;
for(int j=0;j<i;j++)
if(s1[s1.length()-i+j]!=s2[j]) // Check whether the strings are equal
flag=0;
if(flag==true)
return i;
}
return 0;
}
void solve(string str,int length1){
length=max(length1,length); // Maximum length
for(int i=0;i<n;i++){
if(a[i]>=2) // The number of uses must be less than 2
continue;
int c=xianjie(str,s[i]); // Length of overlap
if(c>0){ // To search
a[i]++;
solve(s[i],length1+s[i].length()-c);
a[i]--;
}
}
}
int main(){
cin>>n;
for(int i=0;i<=n;i++){
a[i]=0;
cin>>s[i];
}
solve(' '+s[n],1); //xianjie The overlap of the function needs to be less than the shortest length -1, So add a meaningless full length from the front ‘ ’. This is mandatory xianjie The function compares the last bit
cout<<length;
}
The operation results are as follows
big data 201 tyx
边栏推荐
- 仿牛客技术博客项目常见问题及解答(二)
- [during the interview] - how can I explain the mechanism of TCP to achieve reliable transmission
- 【九阳神功】2017复旦大学应用统计真题+解析
- ArrayList的自动扩容机制实现原理
- 【九阳神功】2020复旦大学应用统计真题+解析
- [hand tearing code] single case mode and producer / consumer mode
- 这次,彻底搞清楚MySQL索引
- . Net6: develop modern 3D industrial software based on WPF (2)
- Change vs theme and set background picture
- 2. First knowledge of C language (2)
猜你喜欢
Leetcode. 3. Longest substring without repeated characters - more than 100% solution
(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。
[hand tearing code] single case mode and producer / consumer mode
2022泰迪杯数据挖掘挑战赛C题思路及赛后总结
A comprehensive summary of MySQL transactions and implementation principles, and no longer have to worry about interviews
5. Function recursion exercise
FAQs and answers to the imitation Niuke technology blog project (I)
扑克牌游戏程序——人机对抗
[during the interview] - how can I explain the mechanism of TCP to achieve reliable transmission
(original) make an electronic clock with LCD1602 display to display the current time on the LCD. The display format is "hour: minute: Second: second". There are four function keys K1 ~ K4, and the fun
随机推荐
3. Number guessing game
canvas基础1 - 画直线(通俗易懂)
Record a penetration of the cat shed from outside to inside. Library operation extraction flag
Inaki Ading
ArrayList的自动扩容机制实现原理
Mortal immortal cultivation pointer-2
Principles, advantages and disadvantages of two persistence mechanisms RDB and AOF of redis
3. Input and output functions (printf, scanf, getchar and putchar)
3.输入和输出函数(printf、scanf、getchar和putchar)
This time, thoroughly understand the MySQL index
C language Getting Started Guide
[modern Chinese history] Chapter 9 test
【头歌educoder数据表中数据的插入、修改和删除】
2.初识C语言(2)
7-9 制作门牌号3.0(PTA程序设计)
7-8 7104 约瑟夫问题(PTA程序设计)
[dark horse morning post] Shanghai Municipal Bureau of supervision responded that Zhong Xue had a high fever and did not melt; Michael admitted that two batches of pure milk were unqualified; Wechat i
实验七 常用类的使用
Poker game program - man machine confrontation
It's never too late to start. The tramp transformation programmer has an annual salary of more than 700000 yuan