题目链接:http://poj.org/problem?id=1750
题意:下面的字符串和上面的字符串字符相等的个数大于上面字符串前空格的个数就多输出一个空格,否则输出字符相等个数的空格,看代码理解吧。
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
char q[100005][15];
int main()
{
ios::sync_with_stdio(0); //注意要关流或用scanf,否则会超时
cin >> q[0];
cout << q[0] << endl;
int u = 0;
int k = 1;
int len;
while (cin >> q[k])
{
len = 0;
int str1 = strlen(q[k]);
int str2 = strlen(q[k - 1]);
for (int j = 0; j < min(str1, str2); j++)
{
if (q[k][j] == q[k - 1][j])
{
len++;
}
else
{
break;
}
}
if (len > u)
{
u++;
//cout<<u<<endl;
}
else
{
u = len;
//cout<<u<<endl;
}
for (int i = 0; i < u; i++)
{
cout << ' ';
}
cout << q[k] << endl;
k++;
}
}