http://www.patest.cn/contests/pat-a-practise/1077
The Japanese language is notorious for its sentence ending particles. Personal preference of such particles can be considered as a reflection of the speaker’s personality. Such a preference is called “Kuchiguse” and is often exaggerated artistically in Anime and Manga. For example, the artificial sentence ending particle “nyan~” is often used as a stereotype for characters with a cat-like personality:
- Itai nyan~ (It hurts, nyan~)
- Ninjin wa iyada nyan~ (I hate carrots, nyan~)
Now given a few lines spoken by the same character, can you find her Kuchiguse?
这个题目就是到着找最长相同子串,没啥难度,用char数组操作很方便,直接给代码
#include <iostream>
#include <string.h>
using namespace std;
bool tryMoveForward(int N, char** sentences, int* posPtr, bool &contFlag)
{
//尝试当前位置的字符是否相同
char chr = sentences[0][posPtr[0]];
bool flag = true;
int i = 0;
while(i < N)
{
if (sentences[i][posPtr[i]] != chr)
{
flag = false;
break;
}
i++;
}
if (flag)
{
//Move forward
for (i = 0; i < N; i++)
{
posPtr[i] --;
if (posPtr[i] <0)
contFlag = false;
}
}
return flag;
}
int main()
{
int N;
cin >> N;
char **sentences = new char*[N]; //存储句子
int* posPtr = new int[N]; //当前位置指针
//int maxSuffixStartPos = 0; //最长子串起始位置(距离最后一个字符)
cin.ignore(1);
for (int i = 0; i < N; i++)
{
sentences[i] = new char[257];
//cin >> sentences[i];
cin.getline(sentences[i], 257);
posPtr[i] = strlen(sentences[i]) - 1;
}
bool contFlag = true;
while (contFlag)
{
bool movFlag = tryMoveForward(N, sentences, posPtr, contFlag);
contFlag &= movFlag;
}
//读取最长字串
int longSufPos = posPtr[0];
if (longSufPos == strlen(sentences[0]) - 1)
cout << "nai";
else
{
char* result = sentences[0] + longSufPos + 1;
cout << result;
}
return 0;
}