XTU OJ 1309 unique string

unique substring

Question description

Given a string, find all non-repeating substrings of length m. For example, for the string "aaab", if we find a substring of length 2, then the order is "aa", "aa", "ab", then the non-repeating substrings are "aa", "ab" ;

enter

The first line is an integer K, indicating the number of samples. The first line of each example is an integer m, indicating the length of the desired substring. The second line is a string. The string consists entirely of lowercase English letters and the length does not exceed 100.

output

Output all non-repeating substrings in lexicographic order, with a blank line at the end of each example.

Sample input

2 
2 
aaab 
3 
aaab

Sample output

aa 
ab  

aaa 
aab

Idea analysis: This question mainly tests the understanding of two-dimensional arrays. Pay attention to the specific meaning of the two spaces in the two-dimensional array. 

#include <stdio.h>
#include <string.h>
char c[1001][1001] = {0};
int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		int m;
		scanf("%d", &m);
		char ch[1001],temp[1001];
		scanf(" %s", ch);
		int len = strlen(ch);
		int flag;
		for ( flag = 0; flag <= len - m; flag++) {
			for (int i = flag, cnt = 0; cnt < m; i++, cnt++) {
				c[flag][cnt] = ch[i];
				//flag代表第几个字串
				//cnt代表该字串的第几位
			}
		}
		//输入没有问题
		//printf("flag:%d\n", flag);
		//将所有字串进行排序
		for (int i = 0; i < flag; i++) {
			for (int j = i + 1; j < flag; j++) {
				if(strcmp(c[i],c[j])>0){
					//比较第i个字符串和第j个字符串
						//如果说i个>j个
						//交换一个位置
					strcpy(temp, c[i]);
					strcpy(c[i], c[j]);
					strcpy(c[j], temp);
				}
			}
		}
		for (int i = 0; i < flag; i++) {
			if (!strcmp(c[i], c[i + 1]))
				continue;//如果说这一个和后一个相同
			for (int j = 0; j < m; j++) {
				printf("%c", c[i][j]);
			}
			printf("\n");
		}
		printf("\n");
		memset(c, '\0', sizeof(c));
		memset(temp, '\0', sizeof(temp));
		memset(ch, '\0',sizeof(ch));
	}
}

Guess you like

Origin blog.csdn.net/qq_24917263/article/details/127721606