779. The longest common suffix string
Title Description
It gives the number string with the longest common suffixes of these strings.
Input Format
It consists of several sets of input components.
The first line of each input is an integer N.
N indicates the end of input is 0, otherwise it will continue back N input lines, each line is a character string (character string excluding the blank).
Length of each string does not exceed 200.
Output Format
A total line, a suffix N is the longest common string (possibly empty).
data range
1 ≤ N ≤ 200
Sample input:
3
baba
aba
cba
2
aa
cc
2
aa
a
0
Sample output:
ba
a
Difficulty: hard |
---|
Time / space restrictions: 1s / 64MB |
Total by: 59 |
The total number of attempts: 103 |
Source: grammar questions |
Algorithms label: String |
AC Code
#include <iostream>
#include <cstdio>
using namespace std;
int n;
string l[201];
int main(){
while(cin>>n){
if(n==0) return 0;
int lm=9999999,la=0;
for(int i=1;i<=n;i++){
cin>>l[i];//先将字符串存储在数组中
int len=l[i].size();
lm=min(len,lm);
}
for(int i=1;i<=lm;i++){
int f=1;
for(int j=1;j<n;j++){
int l1=l[j].size(),l2=l[j+1].size();
if(l[j][l1-i]!=l[j+1][l2-i]){
f=0;
break;
}
}
if(f == 0) break;
else la++;
}
for(int i=la;i;i--) cout<<l[1][l[1].size()-i];
cout<<endl;
}
}
Think
Note the use of the array of strings, the string is also an array.
This question is for the use of the string is more flexible and can then more understanding.