字符串前缀、后缀、子串、字典树
- 格式:docx
- 大小:20.69 KB
- 文档页数:3
return a+b<b+a;;
}
int main()
{
int t;
cin>>t;
string s[8];
while(t)
{inΒιβλιοθήκη n;cin>>n;
for(int i=0;i<n;i++)
{
cin>>s[i];
}
sort(s,s+n,comp);
for(int i=0;i<n;i++)
cout<<s[i];
bool find;
cin >> t;
while ( t-- ) {
cin >> n;
for ( i = 0; i < n; i++ )
cin >> s[ i ];
sort( s, s + n );
find = false;
for ( i = 1; i < n; i++ ) {
if ( s[ i ].find( s[ i - 1 ] ) != string::npos ) {
cout<<"YES"<<endl;
}
return 0;
}
方法2#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string s[ 10000 ];
int t, n;
int i;
}
for(i=0;i<N;i++)
cout<<str[i];
cout<<endl;
}
return 0;
}
方法2:#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
bool comp(string a,string b)
{
int testcase;
string arr[20001];
cin >> testcase;
while(testcase--)
{
int phonenum;
cin >>phonenum;
for(int i =0;i<phonenum;i++)
cin>>arr[i];
sort(arr,arr+phonenum);
bool match=true;
for(int j=0;j<len;j++){
if(numbers[i][j]!=numbers[i+1][j]){
match=false;
break;
}
}
if(match){
cout<<"NO"<<endl;
consistent=false;
break;
}
}
if(consistent)
break;
}
}
}
if (!is)
cout<<"YES"<<"\n";
}
return 0;
}
1198字典序题目要求输出的组合字符串具有最小字典序,其实就是把题目给出的字符串进行排序。考虑输入只有两个字符串s1和s2的情况,要输出s1和s2组合的最小字典序,只需要比较s1+s2和s2+s1这两个字符串哪个的字典序更小即可。扩展到多个字符串的情况也是一样的。
find = true;
break;
}
}
cout << ( find ? "NO": "YES" ) << endl;
}
return 0;
}
方法3
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
排序与每个号码的长短无关(前面相同的较短的较小),而是从首位开始逐位比较。
如对13,123,124,11456使用sort函数进行排序,结果会是11456,123,124,13
这样排序之后如果有不相容的两个号码肯定会出现在相邻的两位了,所以对整个数组所有相邻的两位逐位进行比对即可得到结果。*/
#include<iostream>
cin>>num_of_phone_number;
string numbers[num_of_phone_number];//号码存放数组
for(int i=0;i<num_of_phone_number;i++){//输入号码
cin>>numbers[i];
}
sort(numbers,numbers+num_of_phone_number);//排序
Sicily 1426. Phone List(判断电话号码是否前缀重复)
Sample Input
2
3
911
97625999
91125426
5
113
12340
123440
12345
98346
Sample Output
NO
YES/*用string类型数组读进所有电话号码,然后用sort函数对其进行排序。
cin>>str[i];
for(i=0;i<N;i++)
for(j=i+1;j<N;j++)
{
if(str[i]+str[j]>str[j]+str[i])
str[i].swap(str[j]);
//若str[i]为b,str[j]为ba,如果仅仅是判断str[i]>str[j],那结果是bba,但正确的应该是bab,所以条件要加强为str[i]+str[j]>str[j]+str[i]
//Now if inconsistent numbers exist,they must be adjacent
for(int i=0;i<num_of_phone_number-1;i++){//linear search every two numbers
int len=numbers[i].length();
#include<algorithm>
using namespace std;
int main(){
int testcases;//测试数
cin>>testcases;
while(testcases--){
int num_of_phone_number;//号码数
bool consistent=true;//If two numbers are found inconsistent,it will be false
#include<iostream>
#include <string>
#include<cstdio>
using namespace std;
int main()
{
int T,N,l,i,j;
string str[8];
cin>>T;
for(l=0;l<T;l++)
{
scanf("%d",&N);
for(i=0;i<N;i++)
bool is = false;
for (int j=0;j<phonenum -1;j++)
{
if (arr[j].size()<= arr[j+1].size())
{
if (arr[j] ==arr[j+1].substr(0,arr[j].size()))
{
cout<<"NO"<<endl;
is = true;
cout<<endl;
t--;
}
return 0;
}