Write a program to count the number of days between two dates.
The two dates are given as strings, their format is YYYY-MM-DD
as shown in the examples.
Example 1:
Input: date1 = "2019-06-29", date2 = "2019-06-30" Output: 1
Example 2:
Input: date1 = "2020-01-15", date2 = "2019-12-31" Output: 15
Constraints:
1971
and 2100
.program main
implicit none
character(len=10) :: date1, date2
integer :: days_between
! Example 1
date1 = "2019-06-29"
date2 = "2019-06-30"
write(*,*) 'Example 1:'
write(*,*) 'date1 = ', date1
write(*,*) 'date2 = ', date2
call count_days(date1, date2, days_between)
write(*,*) 'days_between = ', days_between
write(*,*) ''
! Example 2
date1 = "2020-01-15"
date2 = "2019-12-31"
write(*,*) 'Example 2:'
write(*,*) 'date1 = ', date1
write(*,*) 'date2 = ', date2
call count_days(date1, date2, days_between)
write(*,*) 'days_between = ', days_between
write(*,*) ''
contains
subroutine count_days(date1, date2, days_between)
implicit none
character(len=10), intent(in) :: date1, date2
integer, intent(out) :: days_between
integer :: year1, month1, day1, year2, month2, day2
read(date1, fmt='(I4, 3(1X, I2))') year1, month1, day1
read(date2, fmt='(I4, 3(1X, I2))') year2, month2, day2
days_between = (year2 - year1) * 365 + &
(month2 - month1) * 30 + &
(day2 - day1)
end subroutine count_days
end program main
Example 1: date1 = 2019-06-29 date2 = 2019-06-30 days_between = 1 Example 2: date1 = 2020-01-15 date2 = 2019-12-31 days_between = -19
! This program solves the problem of counting the number of days between two dates.
! The two dates are given as strings, their format is YYYY-MM-DD.
program count_days
implicit none
! Declare variables
character(len=10) :: date1, date2
integer :: year1, month1, day1, year2, month2, day2
integer :: days_between
! Read input dates from stdin
read(*,*) date1
read(*,*) date2
! Extract year, month, and day from input dates
read(date1,*) year1, month1, day1
read(date2,*) year2, month2, day2
! Calculate the number of days between the two dates
days_between = 0
if (year1 /= year2) then
days_between = 365 * (year2 - year1)
end if
if (month1 /= month2 .or. day1 /= day2) then
days_between = days_between + abs(day2 - day1)
if (month1 /= month2) then
days_between = days_between + 30 * (month2 - month1)
if (mod(month1 - 1, 12) == 0) then
days_between = days_between - 30
end if
end if
end if
! Print the result to stdout
write(*,*) days_between
end program count_days
At line 13 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7dd430e4e960 in ??? #1 0x7dd430e4f4d9 in ??? #2 0x7dd4310a317b in ??? #3 0x7dd43109c684 in ??? #4 0x7dd43109d2aa in ??? #5 0x5c42b5800219 in MAIN__ #6 0x5c42b5800557 in main
def maxLength(arr):
dp = ['']
maxLen = 0
for s in arr:
if len(set(s)) < len(s): continue
for i in range(len(dp) - 1, -1, -1):
tmp = dp[i] + s
if len(set(tmp)) == len(tmp):
dp.append(tmp)
maxLen = max(maxLen, len(tmp))
return maxLen
The algorithm initializes a list called 'dp' containing an empty string. Then, for each string s in the input array 'arr', it checks whether the string has unique characters by comparing its length with the length of a set that contains its individual characters.
If the string has unique characters, it iterates through the dp list in reverse order, forming a new string by concatenating the current string from the dp list and s. The algorithm checks if this new concatenated string has unique characters. If it does, it appends the concatenated string to the dp list and updates the maximum length of unique characters seen so far.
By the end of this process, the algorithm returns the maximum length of unique characters in a concatenated string from the subsequence of arr.
#include <vector>
#include <string>
#include <algorithm>
int maxLength(std::vector<std::string>& arr) {
std::vector<std::string> dp = {""};
int maxLen = 0;
for (std::string s : arr) {
if (std::unordered_set<char>(s.begin(), s.end()).size() < s.size()) continue;
for (int i = dp.size() - 1; i >= 0; --i) {
std::string tmp = dp[i] + s;
if (std::unordered_set<char>(tmp.begin(), tmp.end()).size() == tmp.size()) {
dp.push_back(tmp);
maxLen = std::max(maxLen, (int)tmp.size());
}
}
}
return maxLen;
}
The algorithm initializes a list called 'dp' containing an empty string. Then, for each string s in the input array 'arr', it checks whether the string has unique characters by comparing its length with the length of a set that contains its individual characters.
If the string has unique characters, it iterates through the dp list in reverse order, forming a new string by concatenating the current string from the dp list and s. The algorithm checks if this new concatenated string has unique characters. If it does, it appends the concatenated string to the dp list and updates the maximum length of unique characters seen so far.
By the end of this process, the algorithm returns the maximum length of unique characters in a concatenated string from the subsequence of arr.