1. 程式人生 > >習題 3-4 週期串 UVa455

習題 3-4 週期串 UVa455

2.解題思路:在長度範圍之內,一個個列舉週期即可,注意週期應能被總長度整除

3.程式碼:

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define max 100+10
char s[max];
int main()
{
	int n, len;
	scanf("%d", &n);
	getchar();
	while (n--)
	{
		getchar();
		gets(s);
		len = strlen(s);
		for (int i = 1; i <= len;i++)
		if (len%i == 0)
		{
			bool ok = true;
			for (int j = i; j < len;j++)
			if (s[j] != s[j%i])
			{
				ok = false;
				break;
			}
			if (ok)
			{
				printf("%d\n", i);
				if (n)
					putchar('\n');
				break;
			}
		}
	}
	return 0;
}
</span>

相關推薦

習題 3-4 週期 UVa455

2.解題思路:在長度範圍之內,一個個列舉週期即可,注意週期應能被總長度整除 3.程式碼: #include<stdio.h> #include<string.h> #incl

習題3-4 週期(Periodic Strings, UVa455)

如果一個字串可以由某個長度為k的字串重複多次得到,則稱該串以k為週期。例 如,abcabcabcabc以3為週期(注意,它也以6和12為週期)。#include "OJ.h" #include <string.h> /* 功能:計算字串的最小週期。 */ i

演算法競賽入門經典(第二版)第三章陣列和字串習題3-4週期

如果一個字串可以由某個長度為k的字串重複多次得到,則稱該串以k為週期。例如abcabcabc以3為週期(注意,它也以6和12為週期)。輸入一個長度不超過80的字串,輸出其最小週期 #include<stdio.h> int main() { char a[100];

習題3-4 週期(Periodic Strings)

/*週期串(暴力窮舉法)*/ #include <stdio.h> #include<strings.h> int main() { char s[85]; scanf("%s",s); int a,len=0; //a是週期長度,len是字串長度 le

演算法競賽入門經典(第2版)習題3-4 週期 Periodic Strings UVa445

這題把我虐哭了。 提交了13次才ac。 演算法本身沒什麼好說的,UVa上html版和pdf版的輸入輸出格式要求不一樣,以html版為準。 而且html版的輸入輸出格式都有點奇葩,不認真看原文細節是ac不了的。 參考了他的例程,我才找到問題的解決辦法。 UVa評測系統中的空

習題3-4 週期 解題報告

題目: 程式碼: #include <iostream> #include <string.h> using namespace std; int main() { char a[85]; int n,k,i,

習題3-4 週期(Periodic Strings)

#include <iostream> #include <stdio.h> #include <string.h> using namespace std; int main() { char s[100]; int

習題 3-4 週期

#include<iostream> #include<cstring> using namespace std; int main(){ int i,j=0,k,t=0;

習題3-4 週期

1.週期的表達方式 %取餘 2.每兩個輸出間空行   while(n--)  if(n) printf("\n") #include <stdio.h> #include <string.h> #define maxn 81 char s[maxn]

習題3.4 最長連續遞增子序列(20 分)浙大版《數據結構(第2版)》題目集

space align font list 格式 ott mar 不能 第一次 給定一個順序存儲的線性表,請設計一個算法查找該線性表中最長的連續遞增子序列。例如,(1,9,2,5,7,3,4,6,8,0)中最長的遞增子序列為(3,4,6,8)。 輸入格式: 輸入

UVa 455 Periodic Strings(習題3-4)

這幾天一直都沒有寫程式碼,主要是有點事情還有就是忙著複習期末考試,所以說最近一段時間都會很少更新了 這道題的話是比較水的,但是因為我在寫程式碼的時候還有有的地方思路沒有想清楚,重複了幾次才過的 一開始想的太簡單了,直接列舉判斷,後來才慢慢修改,還有就是判斷的後面不要加上分號,有時候不細心

python實現周志華西瓜書《機器學習》習題3.4 對比10折交叉驗證和留一法的對率迴歸錯誤率

這道題仍然在抄大神程式碼的基礎上寫註釋,首先感謝原始碼: https://blog.csdn.net/Snoopy_Yuan/article/details/64131129 感想是:sklearn是個好東西,如果沒有現成的驗證方法,光是10折驗證就要造10個表格才行,而用現成的庫,一

3章 SQL 習題 - 3.4

3.4考慮圖3-18中的保險公司資料庫,其中加下劃線的是主碼。為這個關係資料庫構造出如下SQL查詢: person(driver_id, name, address) car(licence, model, year) accident(report_number, date, loca

西瓜書 課後習題3.4 十折交叉和留一法,對率迴歸

import csv import numpy as np def readData(filename): """ :param filename:cvs資料檔案 :return: X1,y1,X2,y2,X3,y3 X: list with s

演算法競賽入門經典(第二版)習題3-4

#include<stdio.h> #include<string.h> char s[85]; int main() { while(scanf("%s",s)) { int len=strlen(s); for(int i=

浙大版《資料結構》習題3.4 最長連續遞增子序列(20 分)

給定一個順序儲存的線性表,請設計一個演算法查詢該線性表中最長的連續遞增子序列。例如,(1,9,2,5,7,3,4,6,8,0)中最長的遞增子序列為(3,4,6,8)。 輸入格式: 輸入第1行給出正整數n(≤105)n(≤105);第2行給出n個整數,其間

習題3.4 最長連續遞增子序列(20 分)

#include<stdio.h> int main() {   int n,i=0;   scanf("%d",&n);   int num[100001]={0,0,};   while(n-->0)       scanf("%d",&

習題3.4 最長連續遞增子序列

習題3.4 最長連續遞增子序列(20 分) 給定一個順序儲存的線性表,請設計一個演算法查詢該線性表中最長的連續遞增子序列。例如,(1,9,2,5,7,3,4,6,8,0)中最長的遞增子序列為(

演算法競賽入門經典:習題3-4 計算器

#include<iostream> #include<string> using namespace std; int main(){ string s; int num = 0; int a[2] = {0}; int j = 0; i

算法競賽入門經典題解——第三章 3-4 周期UVa455

() sca clas pan 可能 span amp tdi ++ 思路:遍歷可能的周期,比較s[k]與s[k%i](其中i為周期) #include <stdio.h> #include <stdlib.h> #include<str