zoukankan      html  css  js  c++  java
  • PAT A1077 Kuchiguse (20)

    晴神书中AC代码

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    using namespace std;
    int n, minLen = 256, ans = 0;
    char s[100][260];
    
    int main() {
    	#ifdef ONLINE_JUDGE
    	#else
    		freopen("1.txt", "r", stdin);
    	#endif // ONLINE_JUDGE
    	scanf("%d", &n); //n是字符串的个数
    	getchar();
    	for(int i = 0; i < n; i++) {
    		cin.getline(s[i], 260);
    		int len = strlen(s[i]);
    		if(len < minLen) minLen = len; //取最小长度
    		for(int j = 0; j < len / 2; j++) { //反转字符串s[i], 转化为求公共后缀
    			char temp = s[i][j]; //交换str[i]与str[len - i - 1]
    			s[i][j] = s[i][len - j - 1];
    			s[i][len - j - 1] = temp;
    		}
    	}
    	for(int i = 0; i < minLen; i++) { //判断所有字符串的第i个字符是否全部相等
    		char c = s[0][i]; //取第一个字符串的第i个字符
    		bool same = true;
    		for(int j = 1; j < n; j++) {	//判断其余字符串的第i个字符是否等于c
    			if(c != s[j][i]) { //只要有一个不等,就停止枚举,说明公共后缀到处为止
    				same = false;
    				break;
    			}
    		}
    		if(same) ans++; //若所有字符串的第i位相等,则计数器ans加1
    		else break;
    	}
    	if(ans) {
    		for(int i = ans - 1; i >= 0; i--) {
    			printf("%c", s[0][i]);
    		}
    	} else {
    		printf("nai"); //不存在公共后缀
    	}
    	return 0;
    }
    
  • 相关阅读:
    map & reduce
    Generator
    切片
    函数参数
    Dict & Set
    list,tuple
    selenium鼠标和键盘操作
    selenium元素定位以及点击事件
    css定位
    xpath
  • 原文地址:https://www.cnblogs.com/isChenJY/p/11400360.html
Copyright © 2011-2022 走看看